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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Newbie JS question.

    Hi, I'm just trying to validate a form and it just isn't working. I'm pretty new to this, but here is my code so far:

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function validation ( form )
    {
    
    	if (form.firstname.value == "") {
    		alert( "Please enter a first name." );
    		form.firstname.focus();
    		return false ;
    	}
    
    	 
    	if (form.lastname.value == "") {
    		alert( "Please enter a last name." );
    		form.lastname.focus();
    		return false ;
    	}
    
    	 
    	if (form.firstname.value == "`1234567890-=[]'#/.,\¬!"£$%^&*()_+{}@~?><|") {
    		alert( "Please enter a valid first name." );
    		form.firstname.focus();
    		return false ;
    	}
    
    	 
    	if (form.lastname.value == "`1234567890-=[]'#/.,\¬!"£$%^&*()_+{}@~?><|") {
    		alert( "Please enter a valid last name." );
    		form.lastname.focus();
    		return false ;
    	}
    
    	return true;
    }
    //-->
    </script>
    this does not work at all, however if i only do one validation on field such as:

    Code:
    <script language="JavaScript" type="text/javascript">
    <!--
    function validation ( form )
    {
    
    	if (form.firstname.value == "") {
    		alert( "Please enter a first name." );
    		form.firstname.focus();
    		return false ;
    	}
    
    }
    //-->
    </script>
    Then it works fine.

    Thanks guys.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Validation of the form if (form.firstname.value == "") { is barely worthy of the name, as even a single space or a ? will return false (i.e. pass the validation).

    A proper name may only contain letters a-z, and perhaps a hyphen (Mary-Lou), a space (Mary Lou) or an apostrophe (O'Reilly).

    Follow this example:-


    Code:
    var fn = document.formname.firstname.value;
    fn = fn.replace(/^\s+|\s+$/g,"");  // strip leading and trailing spaces;
    if (/[^a-z\-\'\s]/gi.test(fn)) {
    alert ("Please enter a valid first name (only A-Z hyphen space and apostrophe allowed)");
    document.formname.firstname.value = "";  // erase invalid entry
    document.formname.firstname.focus();
    return false;
    }

    <script language="JavaScript"> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. Likewise the <!-- and //--> tags have not been needed since IE4.


    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose.



    Quizmaster: In which book is Room 101 a place to be feared?
    Contestant: 101 Dalmations
    Last edited by Philip M; 05-26-2009 at 06:57 PM.

  • Users who have thanked Philip M for this post:

    Pyraine (05-26-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi, thanks for the help.

    Sorry about the title, I assumed the tags were for helping people find the post.

    The validation of the form if (form.firstname.value == "") { was intended to be used alongside other validation, just to make sure that the form was not accidentally or intentionally skipped when it was required. I noticed with the validation you gave me it would pass validation if the field was left blank.

    also I had some issues with document.formname.firstname, I tried replacing it with form.firstname and that seemed to do the trick.

    Thanks for your help though! Using your technique it appears I can validate multiple fields on my form, which is where I was struggling before. Thank you very much.

    Also thanks for informing me of the obsolete code I was using.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Pyraine View Post
    . I noticed with the validation you gave me it would pass validation if the field was left blank.
    Sorry, my bad. You can specify a minimum length for a field as follows:-

    if ((fn.length < 2) || (/[^a-z\-\'\s]/gi.test(fn))) {

  • Users who have thanked Philip M for this post:

    Pyraine (05-26-2009)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    You could be a little less flexible (that RE will, for example, *pass* the name "- - - ' ' -") with maybe
    Code:
    if ( ! ( /^[a-z]+([\-\'\s]?[a-z]+)+$/i.test(fn) ) ) {
    That says "at least one letter followed by an optional delimiter of dash, apostrophe or space followed by at least one more letter". The "delimiter with letter(s) following" can be repeated. Note that this does allow for as simple a name as two letters.

  • Users who have thanked Old Pedant for this post:

    Pyraine (05-26-2009)

  • #6
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks guys I appreciate the help

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    You could be a little less flexible (that RE will, for example, *pass* the name "- - - ' ' -") with maybe
    Code:
    if ( ! ( /^[a-z]+([\-\'\s]?[a-z]+)+$/i.test(fn) ) ) {
    That says "at least one letter followed by an optional delimiter of dash, apostrophe or space followed by at least one more letter". The "delimiter with letter(s) following" can be repeated. Note that this does allow for as simple a name as two letters.
    Yes, but it will also pass zxz'xzx or something equally silly. How is that better than - - - ' ' - ?

    If you want to reduce the scope for idiocy further you can specify that the name field contains at least one vowel and one consonant. (Al, Jo, Lee, Byng). But at the end of the day you cannot stop people entering their names as Xuzyzyzyz or Mickey Mouse. Or of course mis-spelling their name as Phlip or whatever.
    Last edited by Philip M; 05-27-2009 at 08:18 AM.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Yeah, too true. Okay, just need to protect against entering all spaces and entering HTML, I guess.


  •  

    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
    •