...

View Full Version : How to test for string length?



chornbeck
08-10-2006, 03:25 PM
The following function takes three different fields (which combined make up a phone number) and tests it to make sure that all the characters entered are integers. If this is true, it sets form.Phone.value = entirephonenumber. I would like to incorporate an additional test which, after determining that all characters in the string are integers, checks to make sure that the string length is 10. If it is less than 10 or more, I want the entire function to return false and display an error message (the same one is fine..) Any help with getting this accomplished is greatly appreciated. Thanks in advance!



function validateMyForm(form) {



var entirephonenumber = document.entryform.phoneareacode.value + document.entryform.phoneexchange.value + document.entryform.phonenumber.value



if (parseInt(entirephonenumber) !=

entirephonenumber) {

alert('Please enter a valid phone number, numbers only');

return false;

} else {

form.Phone.value = entirephonenumber;

}



return true;

}

vwphillips
08-10-2006, 03:37 PM
function validateMyForm(form) {
var entirephonenumber = document.entryform.phoneareacode.value + document.entryform.phoneexchange.value + document.entryform.phonenumber.value
if (parseInt(entirephonenumber) !=entirephonenumber) {
alert('Please enter a valid phone number, numbers only');
return false;
}
if (entirephonenumber.length!=10){
alert('10 Digits REqd');
return false;
}
form.Phone.value = entirephonenumber;
return true;
}


or


function validateMyForm(form) {
var entirephonenumber = document.entryform.phoneareacode.value + document.entryform.phoneexchange.value + document.entryform.phonenumber.value
if (isNaN(entirephonenumber)) {
alert('Please enter a valid phone number, numbers only');
return false;
}
if (entirephonenumber.length!=10){
alert('10 Digits REqd');
return false;
}
form.Phone.value = entirephonenumber;
return true;
}

chornbeck
08-10-2006, 03:55 PM
Many thanks Vic!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum