Click to See Complete Forum and Search --> : Newbie Validation Q
superdude
02-19-2003, 04:37 AM
Hiya, i have this function:
function telephoneValidator(formObj)
{
if (formObj.Telephone.value.length < 10)
{
alert("Please enter a valid Telephone number. Telephone numbers should be at least 10 digits and no more than 18 digits in length.");
formObj.Telephone.focus();
return (false);
}
return (true);
}
i would like to validate it further by making sure there is a leading 0 (zero) at the beginning of the number (like 01256354212), the digits should not exceed 18, so far i've been able to validate only <10 and finally only numbers allowed.
Can anybody help? Thx
Charles
02-19-2003, 04:58 AM
<input type="text" onchange="if (!/^0\d{9,17}$/.test(this.value)) {alert ('Please enter a valid phone number.'); this.value = '', this.focus()}">
superdude
02-19-2003, 05:23 AM
thanx charles.... unfortunately i am more confused than before ... :o
what you have suggested is validated on the spot in the form, but i would like and prefer to keep it on a separate js file and i am wondering how to integrate what you have suggested in the function i already have done ... :confused:
would it be then:
function telephoneValidator(formObj)
{
if (formObj.Telephone.value (!/^0\d{9,17}$/))
{
alert("Please enter a valid Telephone number. Telephone numbers should be at least 10 digits and no more than 18 digits in length.");
formObj.Telephone.focus();
return (false);
}
return (true);
}
excuse my ignorance... :D
Charles
02-19-2003, 05:28 AM
function telephoneValidator(formObj)
{
if (!/^0\d{9,17}$/.test(formObj.Telephone.value))
{
alert("Please enter a valid Telephone number. Telephone numbers should be at least 10 digits and no more than 18 digits in length.");
formObj.Telephone.focus();
return (false);
}
return (true);
}
superdude
02-19-2003, 05:44 AM
charles thanx once again. it does make more sense the way you put it ;) this time though i get the alert message no matter how many digits i type in or whether i put the leading zero or not
in theory by putting at least 10 digits the alert prompt should not pop up, same if it is 18 digits and again same thing if the 10-18 digits start with 'zero'
any ideas what the prob could be?
Charles
02-19-2003, 06:05 AM
It's hard to say what's wrong without seeing the rest of your work. That's why I gave you a version about which I was certain.
superdude
02-19-2003, 06:39 AM
yes i know what you mean.
<form action="formMAA_end.php" method="post" name="maa_form" id="maa_form" onSubmit="return nameValidator(this) && companyValidator(this) && addressValidator(this) && cityValidator(this) && postcodeValidator(this) && telephoneValidator(this) && emailValidator(this)">
...(all other fields not here to keep the code short)...
<input name="Telephone" type="text" class="input" id="Telephone" size="11" maxlength="18">
<input type="submit" class="input" value="submit">
plus the function you know already ...
does it make sense?
superdude
02-19-2003, 06:55 AM
i've just posted a test page maybe this helps
http://e-kom.port5.com/test.php
superdude
02-19-2003, 09:23 AM
Solved! i forgot to add the bold characters when following your advice. My mistake! ...:rolleyes:
function telephoneValidator(formObj)
{
if (!/^0\d{9,17}$/.test(formObj.Telephone.value))
{
alert("Please enter a valid Telephone number. Telephone numbers should be at least 10 digits and no more than 18 digits in length.");
formObj.Telephone.focus();
return (false);
}
return (true);
}
thanks charles ;)