Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    274
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Script stop working when bringing in 2 new entities

    This code was working fine until I added "["lastname"]["email"]" to it. Why has this problem developed? Was I suppose to separate them or something?

    Code:
    <SCRIPT type="text/javascript">
    function validateForm() {
    	var x=document.forms["form"]["firstname"]["lastname"]["email"].value;
    	if (x=="") {
    		alert ("Fields required for submission have not been filled.");
    		return false;
    	}
    }
    </SCRIPT>
    <FORM action="action.php" method="post" name="form" onsubmit="return validateForm()">
    
    	<INPUT name="firstname" type="text">
    	<BR>
    	<INPUT name="lastname" type="text">
    	<BR>
    	<INPUT name="email" type="text">
    	<BR>
    	<INPUT class="submit" type="submit" value="Submit">
    </FORM>

  • #2
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi, i'm still new to javascript but i think the problem is at
    var x=document.forms["form"]["firstname"]["lastname"]["email"].value;


    I believe there is a better way of doing this
    Code:
    	if (document.forms["form"]["firstname"].value=="" || document.forms["form"]["lastname"].value=="" || document.forms["form"]["email"].value=="" ) {
    		alert ("Fields required for submission have not been filled.");
    		return false;
    	}

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,515
    Thanks
    77
    Thanked 4,379 Times in 4,344 Posts
    Yes.

    But you can do it much shorter, too:
    Code:
    function validateForm(form) 
    {
        if ( form.firstname.value=="" || form.lastname.value=="" || form.email.value=="" ) 
        {
    	alert ("Fields required for submission have not been filled.");
    	return false;
        }
        return true; // really should do this
    }
    ...
    <form action="action.php" method="post" onsubmit="return validateForm(this)">
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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