hi everyone... i'm preparing to complete a validated form through client-side javascript with regular expressions... and yes the form will also be validated server-side as well... anyway, my regex code is problematic, and i was wondering if someone who know about regular expressions really well could have a look at it and tell me what is the problem.
for the NAME, CITY, and PROVINCE/STATE lines, i am trying to only allow for alphabetic strings (more than one string)... however, in testing i found that i can enter "New York City", where as i can not enter "123 #$% 789"... but unfortunately this code will also allow me to enter "123New $%^York 789City", so it's not functioning correctly...
i'm fairly new to regular expressions, and i would love to keep the code in it's current format to only validate on form submit, but either regular expressions is a buggy option, or i'm clearly missing something... (i'd vote on the latter)...
please help if you can... thanks
Code:
function checkform ( form )
{
if (!/[a-zA-Z]+$/.test(form.name.value)) {
alert( "Please enter a valid NAME." );
form.name.focus();
return false ;}
if (!/[\w\s]+$/.test(form.addressline1.value)) {
alert( "Please enter a valid ADDRESS." );
form.addressline1.focus();
return false ;}
if (!/[a-zA-Z]+$/.test(form.city.value)) {
alert( "Please enter a valid CITY." );
form.city.focus();
return false ;}
if (!/[a-zA-Z]+$/.test(form.provincestate.value)) {
alert( "Please enter a valid PROVINCE/STATE." );
form.provincestate.focus();
return false ;}
if (!/[\w\s]+$/.test(form.postalzip.value)) {
alert( "Please enter a valid POSTAL/ZIP CODE." );
form.postalzip.focus();
return false ;}
(etc...)
i'm fairly new to regular expressions, and i would love to keep the code in it's current format to only validate on form submit, but either regular expressions is a buggy option, or i'm clearly missing something... (i'd vote on the latter)...
please help if you can... thanks
Comment