Results 1 to 2 of 2

Thread: Javascript RegExp in Forms

  1. #1
    Join Date
    Aug 2010
    Posts
    6

    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. #2
    Join Date
    Jul 2009
    Posts
    58
    It still gives you an error, because you are submitting the input objects to the validation functions not the input values.

    validate_number(area,"wrong area code") should be: validate_number(area.value,"wrong area code")
    validate_number(phone,"wrong phone") should be: validate_number(phone.value,"wrong phone")
    validate_text(name_surname,"wrong name") should be: validate_text(name_surname.value,"wrong name")


    This won't solve your problem completely though. I can't say why but after validating for every character in a string, it will fail after that even with the correct input. Thus if you have a four digit area code it will validate correctly the first four times and fail on the fifth time you click the validate button. I am not sure why but try it and see. I am sure it has to do with the function re.test() and the pattern. Therefore I used function isNaN() instead, which checks whether a string doesn't contain any numbers (returns true) or if it does (returns false). Here is the final code:

    Code:
    <head>
    	<script type='text/javascript'>
    		function validate_text(field,alerttext) {
    			with(field) {
    	  		//var re = /[a-zA-Z]/g;
    			//if(re.test(field))
    			if(isNaN(field)) { return true;}else{alert(alerttext);return false;}
    			}
    		}
    		function validate_number(field,alerttext){
    	 		 with(field) {		  
    	    		//var re = /[0-9]/g;
    			//if(re.test(field))
            		if(!isNaN(field)) { return true;} else{alert(alerttext);return false;}
    	  		}
    		}
    
    
    
    
    		function validateForm(thisForm){
    			with(thisForm)
    			{
    				if(validate_number(area.value,"wrong area code") == false) {
    		
    					area.focus();
    					return false;
    				}
    				if(validate_number(phone.value,"wrong phone") == false) {
    		
    					phone.focus();
    					return false;
    				}
    				if(validate_text(name_surname.value,"wrong name") == false) {
    		
    					name_surname.focus();
    					return false;
    				}
    			}
    		}
    	</script>
    </head>
    
    <body>
    <form action="" method="post">
    <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:5px;" maxlength="7" size="10" name="phone" />
    <b>area:</b>
    <input type="text" style="margin-left:19px;" maxlength="4" size="4" name="area" /> 
    <br /><br />
    <input type="button" value="validate" onclick="javascript:validateForm(this.form);" />
    <br /><br />
    </form>
    </body>

Similar Threads

  1. Forms in JavaScript Game
    By MatrixClaw in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 07 Dec 2009, 10:15 PM
  2. JavaScript Collector
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 03:28 AM
  3. JavaScript library
    By JavaScriptBank in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 09 Jul 2009, 01:26 AM
  4. element specific javascript
    By gumbo in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 04 Dec 2005, 06:25 PM
  5. element specific javascript
    By gumbo in forum Web Design, HTML Reference and CSS
    Replies: 0
    Last Post: 04 Dec 2005, 06:23 PM

Posting Permissions

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