[Javascript] regex example & code sample

Chris T christ at saeweb.com
Fri Apr 30 12:11:43 CDT 2004


> I won't have time to look at your script logic until later today,
> but very quickly my first suggestion is that, instead of this syntax:
>
> onsubmit="if (validatePass('frmCheckPass', 'txtOldPassword',
> 'txtNewPassword1', 'txtNewPassword2') == false){ return false;}"

Good catch. In looking at the code real quick, and thinking about its
possible usage, I would even make it easier by just calling the function
validatePass() without parameters to get it down to:
onSubmit="return validatePassword()"

It's easier on the eyes for futre coders (or for when you yourself have to
revisit this code at a later date). That would avoid the need for eval
statements.


=========================
> >             //declare variables we will use
> >             var returnValue = true;  //will decide whether or not to
submit
> >the form
> >
> >             var strOldPass = eval('document.' + formName + '.' + oldPass
+
> >'.value');
> >             var strNewPass1 = eval('document.' + formName + '.' +
newPass1
> >+ '.value');
> >             var strNewPass2 = eval('document.' + formName + '.' +
newPass2
> >+ '.value');

=========================

Personally, I never use eval() but would rather access the elements in their
various collections. Collections are your friend :)

For instance, if you wanted to stick with passing in the parameters, you
could easily do something like this:

var objForm = document.forms[formName]
var objElOldPass = objForm.elements[oldPass]
var objElNewPass1 = objForm.elements[oldPass]
var objElNewPass2 = objForm.elements[oldPass]

Something like this gives you more flexibility because you have all the
properties of the element open to your scripting, rather than specific
properties - the .value in your case.

Say for example you wanted to highlight each field that failed validation.
You could then change it's .class property dynamically. With your previous
code, you'd have to point a new reference to that property.

Otherwise though, good stuff. Keep it coming.

Chris Tifer
http://emailajoke.com




More information about the Javascript mailing list