A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Validate textfields before submit

  1. #1
    Member
    Join Date
    Nov 2014
    Posts
    34

    Validate textfields before submit

    Hi. I have this form and I want it to display "*required field" beside the textfield if it is blank. If the Submit button is clicked but there are blank fields, it should not submit. Do you mind showing me how?

    I don't know where to put the validations. This swf is put in a php page so here's some part of it

    Code:
    var fllname:TextField;
    var address:TextField;
    var ContactNo:TextField;
    var quantity:TextField;
    var otrack:TextField;
    
    
    btnSubmit1.addEventListener(MouseEvent.CLICK, submit);
    
    function submit(e:MouseEvent):void{
    	var urlvars: URLVariables = new URLVariables;
    
    	urlvars.fllname = fllname.text;
    	urlvars.Oadd = address.text;
    	urlvars.ContactNo = ContactNo.text;
    	urlvars.oquantiy = quantity.text;
    	urlvars.otrack = otrack.text;
    	urlvars.cake = txtCake.text;
    	urlvars.frosting = txtFrosting.text;
    	urlvars.topping = txtToppings.text;
    	urlvars.topping2 = txtToppings2.text;
    	urlvars.filling = txtFilling.text;
    	urlvars.amt = lblAmount.text;
    	
    	var urlreq:URLRequest = new URLRequest("http://localhost/MCC/order.php");
    	urlreq.method = URLRequestMethod.POST;
    	urlreq.data = urlvars;
    	
    	var loader : URLLoader = new URLLoader;
    	loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    	
    	loader.load(urlreq);
    	nextFrame();
     	
    }

  2. #2
    Junior Member
    Join Date
    Oct 2008
    Posts
    13
    which fields would you like to be required?

  3. #3
    Member
    Join Date
    Nov 2014
    Posts
    34
    These

    fllname:TextField;
    address:TextField;
    ContactNo:TextField;
    quantity:TextField;

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    With the code you have you can do a simple check on each field, I havent filled in all of them , I will let you decide which method you prefer. length or ==
    PHP Code:
    btnSubmit1.addEventListener(MouseEvent.CLICKdoCheck);

    function 
    doCheck(e:MouseEvent):void
    {
        if (
    fllname.text == "")
        {
            
    trace("Need box 1");
        }
        else if (
    address.text.length 1)
        {
            
    trace("Need box 2");
        }
        else if (
    ContactNo.text)
        {
            
    trace("Need box 3");
        }
        else if (
    quantity.text)
        {
            
    trace("Need box 4");
        }
        else if (
    otrack.text)
        {
            
    trace("Need box 5");
        }
        else if (
    txtCake.text)
        {
            
    trace("Need box 6");
        }
        else if (
    txtFrosting.text)
        {
            
    trace("Need box 7");
        }
        else if (
    txtToppings.text)
        {
            
    trace("Need box 8");
        }
        else if (
    txtToppings2.text)
        {
            
    trace("Need box 9");
        }
        else if (
    txtFilling.text)
        {
            
    trace("Need box 10");
        }
        else if (
    lblAmount.text)
        {
            
    trace("Need box 11");
        }
        else
        {
            
    trace("All OK");
            
    submit();
        }
    }

    function 
    submit():void
    {
        var 
    urlvars:URLVariables = new URLVariables();
        
    urlvars.fllname fllname.text;
        
    urlvars.Oadd address.text;
        
    urlvars.ContactNo ContactNo.text;
        
    urlvars.oquantiy quantity.text;
        
    urlvars.otrack otrack.text;
        
    urlvars.cake txtCake.text;
        
    urlvars.frosting txtFrosting.text;
        
    urlvars.topping txtToppings.text;
        
    urlvars.topping2 txtToppings2.text;
        
    urlvars.filling txtFilling.text;
        
    urlvars.amt lblAmount.text;

        var 
    urlreq:URLRequest = new URLRequest("http://localhost/MCC/order.php");
        
    urlreq.method URLRequestMethod.POST;
        
    urlreq.data urlvars;

        var 
    loader:URLLoader = new URLLoader  ;
        
    loader.dataFormat URLLoaderDataFormat.VARIABLES;

        
    loader.load(urlreq);
        
    nextFrame();


  5. #5
    Member
    Join Date
    Nov 2014
    Posts
    34
    Hi...

    It's giving me this error
    1136: Incorrect number of arguments. Expected 1.

    it highlights the line of submit();

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    well post your whole code or attach the fla (second one better0

    actually remove whatever is inside of the brackets on submit function

  7. #7
    Member
    Join Date
    Nov 2014
    Posts
    34
    test.fla

    Here

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    you didnt put any of the check code in it.

    put the code in so we get thte error you said you have then we can fix that error

  9. #9
    Member
    Join Date
    Nov 2014
    Posts
    34
    Oh yeah. I removed what's inside the submit function. It worked now but I want the "Need box __" to appear beside the field.

  10. #10
    Member
    Join Date
    Nov 2014
    Posts
    34
    I'm editing now. sorry for that.

    What's the difference between length and the other one?

  11. #11
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Length means the textfield letter amount and == means is it equal to

  12. #12
    Member
    Join Date
    Nov 2014
    Posts
    34
    I see. Thanks.How to display the "Need..." beside the field?

  13. #13
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    where is your newer file

  14. #14
    Member
    Join Date
    Nov 2014
    Posts
    34

  15. #15
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Scene 1, Layer 'AS', Frame 1, Line 10 1120: Access of undefined property cakeSelected.
    Scene 1, Layer 'AS', Frame 1, Line 11 1120: Access of undefined property frostingSelected.
    Scene 1, Layer 'AS', Frame 1, Line 12 1120: Access of undefined property toppingsSelected.
    Scene 1, Layer 'AS', Frame 1, Line 13 1120: Access of undefined property toppings2Selected.
    Scene 1, Layer 'AS', Frame 1, Line 14 1120: Access of undefined property fillingSelected.


    you need to fix those errors first

    you seem to have other code in your files that you do not tell us about in the first place, it could save time for both of us.

    Yo also seem to not post the whole file, is it a puzzle?
    Last edited by fruitbeard; 01-22-2015 at 09:13 AM.

  16. #16
    Member
    Join Date
    Nov 2014
    Posts
    34
    oh. I sent you the frame for the scene i just need. You can disregard those.

  17. #17
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi

    with the code from the file you sent, I havent changed anything other than to comment out what i need to prevent errors.
    PHP Code:
    stop();

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.events.TextEvent;


    var 
    sCake:String// = cakeSelected;
    var sFrosting:String// = frostingSelected;
    var sToppings:String// = toppingsSelected;
    var sToppings2:String// = toppings2Selected;
    var sFilling:String// = fillingSelected;

    var bCakeInt:int;
    var 
    bFrostingInt:int;
    var 
    bToppingsInt:int;
    var 
    bToppings2Int:int;
    var 
    bFellingInt:int;

    var 
    fllName:String;
    var 
    addressO:String;
    var 
    contactNO:String;
    var 
    quantityO:String;
    var 
    tracking:String;
    var 
    amtStr:String;

    //var fllname:TextField;
    //var address:TextField;
    //var ContactNo:TextField;
    //var quantity:TextField;
    //var otrack:TextField;

    stage.focus fllname;
    ContactNo.maxChars 12;
    fllname.multiline false;
    address.multiline false;
    ContactNo.multiline false;
    quantity.multiline false;

    fllname.text "";
    address.text "";
    ContactNo.text "";
    quantity.text "";

    fllname.restrict "^0-9";
    quantity.restrict "1-9";
    ContactNo.restrict "0-9";

    reqField.text "";

    function 
    generateRandomString(strlen:Number):String
    {
        var 
    chars:String "bcdefghijklmnpqrstvwxyzABCDEFGHIJKLMNPQRSTVWXYZ123456789";
        var 
    num_chars:Number chars.length 1;
        var 
    randomChar:String "";

        for (var 
    i:Number 0strleni++)
        {
            
    randomChar +=  chars.charAt(Math.floor(Math.random() * num_chars));
        }
        return 
    randomChar;
    }

    otrack.text String(generateRandomString(8));


    quantity.restrict "0-9";


    /*
    if (fllName != null)
    {
        fllname.text = String(fllName);
    }
    else
    {
        fllName = "";
    }

    if (addressO != null)
    {
        address.text = String(addressO);
    }
    else
    {
        addressO = "";
    }

    if (contactNO != null)
    {
        ContactNo.text = String(contactNO);
    }
    else
    {
        contactNO = "";
    }

    if (quantityO != null)
    {
        quantity.text = String(quantityO);
        if (amtStr != null)
        {
            lblAmount.text = amtStr;
        }
    }
    else
    {
        quantityO = "";
    }

    if (tracking != null)
    {
        otrack.text = String(tracking);
    }
    else
    {
        tracking = "";
    }
    */

    // Switch case for cakes 
    switch (sCake)
    {
        case 
    "bChoco" :
            
    txtCake.text "Chocolate";
            
    bCakeInt 0;
            break;
        case 
    "bVanilla" :
            
    txtCake.text "Vanilla";
            
    bCakeInt 1;
            break;
        case 
    "bMocha" :
            
    txtCake.text "Mocha";
            
    bCakeInt 2;
            break;
        case 
    "bCaramel" :
            
    txtCake.text "Caramel";
            
    bCakeInt 3;
            break;
        default :
            
    txtCake.text "Chocolate";
            
    bCakeInt 0;
            break;
    }

    // Switch case for frosting 
    switch (sFrosting)
    {
        case 
    "fChoco" :
            
    txtFrosting.text "Chocolate";
            
    bFrostingInt 0;
            break;
        case 
    "fVanilla" :
            
    txtFrosting.text "Vanilla";
            
    bFrostingInt 1;
            break;
        case 
    "fBubblegum" :
            
    txtFrosting.text "Bubble gum";
            
    bFrostingInt 2;
            break;
        case 
    "fStrawberry" :
            
    txtFrosting.text "Strawberry";
            
    bFrostingInt 3;
            break;
        default :
            
    txtFrosting.text "Chocolate";
            
    bFrostingInt 0;
            break;
    }

    // Switch case for Toppings 1
    switch (sToppings)
    {
        case 
    "tChoco" :
            
    txtToppings.text "Choco Shavings";
            
    bToppingsInt 0;
            break;
        case 
    "tWhiteChoco" :
            
    txtToppings.text "White Choco Shavings";
            
    bToppingsInt 1;
            break;
        case 
    "tBlackNWhite" :
            
    txtToppings.text "Black and white";
            
    bToppingsInt 2;
            break;
        case 
    "tChili" :
            
    txtToppings.text "Chili";
            
    bToppingsInt 3;
            break;
        case 
    "tNone" :
            
    txtToppings.text "None";
            
    bToppingsInt 4;
            break;
        default :
            
    txtToppings.text "Choco Shavings";
            
    bToppingsInt 0;
            break;
    }

    // Switch case for Toppings 2
    switch (sToppings2)
    {
        case 
    "ttChoco" :
            
    txtToppings2.text "Choco Shavings";
            
    bToppings2Int 0;
            break;
        case 
    "ttWhiteChoco" :
            
    txtToppings2.text "White Choco Shavings";
            
    bToppings2Int 1;
            break;
        case 
    "ttBlackNWhite" :
            
    txtToppings2.text "Black and white";
            
    bToppings2Int 2;
            break;
        case 
    "ttChili" :
            
    txtToppings2.text "Chili";
            
    bToppings2Int 3;
            break;
        case 
    "ttNone" :
            
    txtToppings2.text "None";
            
    bToppings2Int 4;
            break;
        default :
            
    txtToppings2.text "Choco Shavings";
            
    bToppings2Int 0;
            break;
    }


    // Switch case for Filling 
    switch (sFilling)
    {
        case 
    "fillChoco" :
            
    txtFilling.text "Chocolate";
            
    bFellingInt 0;
            break;
        case 
    "fillStrawberryjam" :
            
    txtFilling.text "Strawberry Jam";
            
    bFellingInt 1;
            break;
        case 
    "fillBlueberry" :
            
    txtFilling.text "Blueberry";
            
    bFellingInt 2;
            break;
        case 
    "fillNofilling" :
            
    txtFilling.text "None";
            
    bFellingInt 3;
            break;
        default :
            
    txtFilling.text "Chocolate";
            
    bFellingInt 0;
            break;
    }

    var 
    bCInt:int;
    var 
    bFrInt:int;
    var 
    bTInt:int;
    var 
    bToInt:int;
    var 
    bFeInt:int;

    quantity.addEventListener(Event.CHANGEchangeListenerfalse0true);
    var 
    totalAmt:int;
    function 
    changeListener(e:Event):void
    {

        var 
    withFelling:int 55;
        var 
    withoutFelling:int 50;

        if (
    txtFilling.text == "None")
        {

            
    totalAmt withoutFelling int(quantity.text);
        }
        else
        {
            
    totalAmt withFelling int(quantity.text);
        }

        
    lblAmount.text String(totalAmt);

    }

    btnBack.addEventListener(MouseEvent.CLICKbackrward);

    function 
    backrward(event:MouseEvent)
    {
        
    bCInt bCakeInt;
        
    bFrInt bFrostingInt;
        
    bTInt bToppingsInt;
        
    bToInt bToppings2Int;
        
    bFeInt bFellingInt;
        
        
    fllName fllname.text;
        
    addressO address.text;
        
    contactNO ContactNo.text;
        
    quantityO quantity.text;
        
    tracking otrack.text;
        
    amtStr lblAmount.text;
        
    prevFrame();
    }

    btnSubmit1.addEventListener(MouseEvent.CLICKdoCheck);

    function 
    doCheck(e:MouseEvent):void
    {
        
    reqField.text "";
        if (
    fllname.text == "")
        {
            
    trace("Need box 1");
            
    reqField.text "Need box 1";
        }
        else if (
    address.text.length 1)
        {
            
    trace("Need box 2");
            
    reqField.text "Need box 2";
        }
        else if (
    ContactNo.text == "")
        {
            
    trace("Need box 3");
            
    reqField.text "Need box 3";
        }
        else if (
    quantity.text == "")
        {
            
    trace("Need box 4");
            
    reqField.text "Need box 4";
        }
        else
        {
            
    trace("All OK");
            
    reqField.text "";
            
    submit();
        }
    }

    function 
    submit():void
    {
        var 
    urlvars:URLVariables = new URLVariables();

        
    urlvars.fllname fllname.text;
        
    urlvars.Oadd address.text;
        
    urlvars.ContactNo ContactNo.text;
        
    urlvars.oquantiy quantity.text;
        
    urlvars.otrack otrack.text;
        
    urlvars.cake txtCake.text;
        
    urlvars.frosting txtFrosting.text;
        
    urlvars.topping txtToppings.text;
        
    urlvars.topping2 txtToppings2.text;
        
    urlvars.filling txtFilling.text;
        
    urlvars.amt lblAmount.text;

        var 
    urlreq:URLRequest = new URLRequest("http://localhost/MCC/order.php");
        
    urlreq.method URLRequestMethod.POST;
        
    urlreq.data urlvars;

        var 
    loader:URLLoader = new URLLoader  ;
        
    loader.dataFormat URLLoaderDataFormat.VARIABLES;

        
    loader.load(urlreq);
        
    nextFrame();


    i'm sure you can figure out what goes where

    I did remover one error textfield and just use one error Textfield at the top called reqField, one text box for each and every error as it happens
    Last edited by fruitbeard; 01-22-2015 at 09:35 AM.

  18. #18
    Member
    Join Date
    Nov 2014
    Posts
    34
    Works fine but just one thing. I just changes what's inside the "__" to "All fields required"...I don't know why it doesn't show all the letters.

    jj.png

  19. #19
    Member
    Join Date
    Nov 2014
    Posts
    34
    I changed the Classic text to TLF text and it displayed every letter. Thank you so much

  20. #20
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I'm not sure you needed the change all text to tlf text, however you did have a mixtrue of device fonts and anti-alias for animation fonts,
    i personally prefer to use anti-alias for readability (looks better).

    It always helps to embed fonts when you use dynamic or input textfields

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center