[Javascript] Form validation
Cutter (JavaScript List)
java.script at seacrets.com
Tue Jan 20 08:53:14 CST 2004
In the document with the form I call my external script with the function:
script language="JavaScript" src="../js/valForm.js" type="text/javascript"
In the same document I create my array:
fields = new
Array('firstname','lastname','email','userlogin','password','verifypw','addr1','city','state','zip','phoneevn');
In the onSubmit of the form I call the function:
onSubmit="return valForm(fields,this.form)"
And the function (in the external file) looks like this:
function valForm(arrFields,form)
{
re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/; // email
validation regex
for (i = 0;i < arrFields.length; i++)
{
fieldName = form + '.' + arrFields[i];
sName = '' + arrFields[i] + ''; // trying this now since
arrFields[i] didn't work in elements[]
if ( fieldName.options )
val = fieldName.options(fieldName.selectedIndex).value;
else
val = fieldName.value;
if ( val == null || val == 0 )
{
alert('You forgot the following required information:\n' +
arrFields[i] + '');
form.elements[sName].focus();
return false;
}
if ( array[i] == 'email' )
{
if( !re.test(val) )
{
alert('You must provide a valid e-mail address.');
form.elements[sName].focus();
form.elements[sName].select();
return false;
}
}
return true;
}
}
And that's it for now. Just can't get this working yet...
Cutter
Chris Tifer wrote:
>First, what is "form"?
>
>If that's the exact syntax you're using, I'd have to suggest that form is
>not pointing to a form. If you have a name on your form (for instance
>"myForm"), do this:
>
>var objForm = document.forms["myForm"]
>
>Then try
>
>objForm.elements[arrFields[i]].focus()
>
>form by itself is nothing...
>
>If that still doesn't work, show us your exact code and I bet we can solve
>it.
>
>Chris Tifer
>
>----- Original Message -----
>From: "Cutter (JavaScript List)" <java.script at seacrets.com>
>To: "[JavaScript List]" <javascript at LaTech.edu>
>Sent: Tuesday, January 20, 2004 9:41 AM
>Subject: Re: [Javascript] Form validation
>
>
>
>
>>Chris,
>>
>>Thanks, but I'm still languishing here. I tried
>>
>>form.elements[arrFields[i]].focus();
>>and
>>form.elements[sName].focus();
>>
>>Both times it gave me the error "Error:'elements' is null or not an
>>object". Any ideas? Anyone? Anyone?
>>
>>Cutter
>>
>>Chris Tifer wrote:
>>
>>
>>
>>>>form.array[i].focus();
>>>>
>>>>
>>>>
>>>>
>>>--------------------------
>>>As long as it's an input of some sort, you can always do:
>>>
>>>form.elements[array[i]].focus()
>>>
>>>Now I'm assuming array[i] just refers to to a string. Not to an actual
>>>element. If it's an element, it'd probably error out.
>>>
>>>And I am also assuming that you did not name your array, array[].
>>>
>>>
>Correct?
>
>
>>>If so, call it something like arrFields or something else.
>>>
>>>
>>>
>>>_______________________________________________
>>>Javascript mailing list
>>>Javascript at LaTech.edu
>>>https://lists.LaTech.edu/mailman/listinfo/javascript
>>>
>>>
>>>
>>>
>>_______________________________________________
>>Javascript mailing list
>>Javascript at LaTech.edu
>>https://lists.LaTech.edu/mailman/listinfo/javascript
>>
>>
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript
>
>
More information about the Javascript
mailing list