Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2010, 11:02 AM   PM User | #1
Experimental
New to the CF scene

 
Join Date: Mar 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Experimental is an unknown quantity at this point
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 />....
Experimental is offline   Reply With Quote
Old 08-26-2010, 02:07 PM   PM User | #2
Arty Effem
Banned

 
Join Date: May 2006
Location: England
Posts: 664
Thanks: 0
Thanked 84 Times in 84 Posts
Arty Effem can only hope to improve
if(re.test(field.value)) Repeat throughout.
Arty Effem is offline   Reply With Quote
Old 08-26-2010, 08:30 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:14 AM.


Advertisement
Log in to turn off these ads.