...

View Full Version : form input length validation



turpentyne
03-11-2012, 10:33 AM
I'm trying to figure out how to add a length paramater to this function. Not quite sure how to do it. here's what I tried:




var phone = document.getElementById('phone');

if(isNumericdashes(phone, "Please enter a valid phone number")){
return true;
}

function isNumericdashes(elem, helperMsg){
var numericExpression = /^[0-9\(\)-]+$/;
if(elem.value.match(numericExpression) &&(elem.length > 9)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

Philip M
03-11-2012, 10:45 AM
if ( (elem.value.match(numericExpression) ) && (elem.length > 9) ){

Missing brackets.

Logic Ali
03-11-2012, 07:07 PM
var phone = document.getElementById('phone');

if(isNumericdashes(phone, "Please enter a valid phone number")){
return true;
}
Presumably the above is code from another function, but you don't seem to have provided a way to return false..

The test you need is:

if( elem.value.match( numericExpression ) && elem.value.length > 9 )
Also you may need to escape the '-' : /^[0-9\(\)\-]+$/

felgall
03-11-2012, 09:15 PM
PAlso you may need to escape the '-' : /^[0-9\(\)\-]+$/

- doesn't need to be escaped if it is the last character inside the []



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum