[Javascript] Form validation

Chris Tifer christ at saeweb.com
Tue Jan 20 10:18:53 CST 2004


This is going off-topic, but for some reason I just hate eval(). I don't
believe I've ever found a need to use it.  There's always some other way...

Chris Tifer

----- Original Message ----- 
From: "allard schripsema" <allard-schripsema at procergs.rs.gov.br>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Tuesday, January 20, 2004 12:14 PM
Subject: Re: [Javascript] Form validation


> here is your version working:
> The main problem was that you were working with a string, not an object.
> Here i used "eval" to get the object, but you could also use
> getElementById(sName)
>
>
> <html>
> <head>
>  <title>Untitled</title>
> </head>
> <script>
> function valForm(arrFields,form){
>     re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/; // email
> validation regex
>      for (i = 0;i < arrFields.length; i++) {
>         fieldName = eval(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] + '');
>             fieldName.focus();
>             return false;
>         }
>         if ( arrFields[i] == 'email' ){
>             if( !re.test(val) ){
>                 alert('You must provide a valid e-mail address.');
>                 fieldName.focus();
>                 fieldName.select();
>                 return false;
>             }
>         }
>     }
>  alert("all OK")
>  return true;
> }
> </script>
> <body>
> <form name="formLogon">
> userlogin: <input type="text" id="userlogin"><br>
> password:  <input type="password" id="password"><br>
> email: <input type="text" id="email"><br>
> <input type="button" value ="test form"
> onclick="valForm(['userlogin','password','email'],'formLogon')"
> </form>
> </body>
> </html>
>
> ----- Original Message ----- 
> From: "Cutter (JavaScript List)" <java.script at seacrets.com>
> To: "[JavaScript List]" <javascript at LaTech.edu>
> Sent: Tuesday, January 20, 2004 11:53 AM
> Subject: Re: [Javascript] Form validation
>
>
> > 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','addr
> 1','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
> > >
> > >
> >
> >
> > _______________________________________________
> > 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