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
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript RegExp in Forms

    I cannot get the result right. I write area and phone by numbers but it still gives me error

    Code:
    function validate_text(field,alerttext) {
    	with(field) {
    		
    	  var re = /[a-zA-Z]/g;
      	  if(re.test(field)) { return true;}else{alert(alerttext);return false;}
    	}
    }
    function validate_number(field,alerttext){
    	  with(field) {
    		  
    	    var re = /[0-9]/g;
            if(re.test(field)) { return true;}else{alert(alerttext);return false;}
    	  }
    }
    
    
    
    function validateForm(thisForm){
    	with(thisForm)
    	{
    		if(validate_number(area,"wrong area code") == false) {
    		
    			area.focus();return false;	
    		}
    		if(validate_number(phone,"wrong phone") == false) {
    		
    			phone.focus();return false;	
    		}
    		if(validate_text(name_surname,"wrong name") == false) {
    		
    			name_surname.focus();return false;	
    		}
    
    	}
    }
    Code:
    ...<b>Name Surname :</b><input style="margin-left:10px;" type="text" name="name_surname"/>
    <br /><br />
        <b>phone :</b><input type="text" style="margin-left:19px;" maxlength="4" size="4" name="area" /> 
    -<input type="text" style="margin-left:5px;" maxlength="7" size="10" name="phone" /><br /><br />....

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    if(re.test(field.value)) Repeat throughout.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    That validation is useless:
    Code:
    function validate_number(field,alerttext){
    	  with(field) {
    		  
    	    var re = /[0-9]/g;
            if(re.test(field.value)) { return true;}else{alert(alerttext);return false;}
    	  }
    }
    That will *PASS* any field value that has even ONE digit in it!

    So if the user enters "a%*(#)!4xyz" it will PASS because of the '4" in there.

    A better test might be:
    Code:
    function validate_number(field,alerttext)
    {
           var re = /[^0-9]/;
    
           if( ! re.test(field.value) ) return true;
    
           alert(alerttext);
           return false;
    }
    Which will barf if it finds *ANY* non-digit characters.

    But that's not a good test for phone numbers, as people are used to putting in ( ) and - and . and maybe + into phone numbers.

    So you should think more about what you really want to allow and really want to reject.

    (Note that your test of the name field is likewise flawed. A name such as "!@#$$%^&()_+-=[]z1234567890" would be *accepted* because of the single "z" in there.)
    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
    •