[Javascript] regex example & code sample

Paul Novitski paul at novitskisoftware.com
Fri Apr 30 11:39:40 CDT 2004


Shawn,

Welcome.  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;}"

you use simply:

onsubmit="return validatePass('frmCheckPass', 'txtOldPassword', 
'txtNewPassword1', 'txtNewPassword2')"

Since your validatePass() function returns a true/false Boolean, you can 
simply return the function's return value to the submit process to control 
whether it's executed.

I tried removing the 'return' keyword as well but it seems to be required 
in order to affect the form submission.  If anyone can explain why I'd be 
appreciative.

Cheers,
Paul


At 08:59 AM 4/30/2004, shawn_milochik at godivachoc.com wrote:




>I'm glad to have received so many replies from the group already.  I'm
>going to be so bold as to post some of my code on my first day of
>membership.
>
>This is a function for validating a new password, and it happens to be
>pretty heavily commented, because I wrote it for a non-programmer
>for him to use in an existing page.  It is designed to be added into the
><form> tag as follows:
>onsubmit="if (validatePass('frmCheckPass', 'txtOldPassword',
>'txtNewPassword1', 'txtNewPassword2') == false){ return false;}"
>
>The purpose of this is that the existing page could not be modified much,
>as it will be regularly updated when the vendor (MicroStategy) comes
>out with new releases of the code, and we needed to be able to easily put
>this functionality back in.
>
>I am posting this to the group because it has some basic regex stuff in it.
>I am also going to post another script later, which I use
>to show the current time in various countries around the world on an HTML
>page, using only Javascript.  I hope someone finds
>this stuff interesting or useful.
>
>Comments/improvements welcome.
>
>
>Shawn
>
>
>
>          //password validation.js
>          function validatePass(formName, oldPass, newPass1, newPass2){
>             //function written on September 18th, 2003
>             //by Shawn Milochik  Shawn_Milochik at godivachoc.com
>             //
>             //Revisions:  none
>             //
>
>             //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');
>             var errMessage = '';
>
>             alert(strNewPass1 + ', ' + strNewPass2);
>
>             var minLength = 7; // change this to change the minimum
>password length
>             var strUID = document.location + ''; //get the querystring from
>the browser so we can get the Uid
>
>             //Time-limited test stuff
>             //replace the actual querystring with the sample
>             //pulled from the real page
>             if (new Date('9/26/2003 15:00:00') > new Date()){
>                alert('testing mode, using fake querystring');
>                strUID =
>'http://gebis/mstr7/ChangePassword.asp?Server=GDRDWEB01&Project=GEBIS+%28Production%29&ProjectId=7FFAF9AC11D4F601500083903038D204&Port=0&Uid=dwuser&UMode=1';
>             }
>
>
>
>
>             //get the Uid from the querystring, and put it in strUID
>             //of the Uid in the querystring.
>             strUID = strUID.replace(/^.*\&Uid=(\w+)\&.*$/, "$1");
>
>             //  Regular Expression translation for above:
>             //       ^ = beginning of the line
>             //       . = any character
>             //       * = any number of the preceeding value, including zero
>             //       \& = &  (& is a special character, and needs to be
>escaped)
>             //       Uid = find the string "Uid"
>             //       \w = a word character (a-z, A-Z, 0-9, _)
>             //       + = one or more consecutive positions
>             //       \& = &  (& is a special character, and needs to be
>escaped)
>             //       . = any character
>             //       * = any number of the preceeding value, including zero
>             //       $ = the end of the line
>             //
>             //       Using parenthesis around part of the expression allows
>us to use
>             //       the matching text in the replace function.  In this
>case, (\w+)
>             //       allowed us to return whatever \w+ matched as $1.  If
>we had used
>             //       another set of parenthesis, we could refer to the
>second one as
>             //       $2, and so on.
>             //
>             //See "Mastering Regular Expressions" by Jeffrey Friedl for all
>this and more.
>             //
>
>
>
>             //**********************************************************
>             //  Rule:  Must contain both upper and lower case
>             //**********************************************************
>             //Check for all lower-case
>             if (strNewPass1.toLowerCase() == strNewPass1){
>                //alert('New password must contain at least one upper-case
>character.');
>                errMessage = errMessage + '\n' + 'New password must contain
>at least one upper-case character.';
>                returnValue = false;
>             }
>
>             //Check for all upper-case
>             if (strNewPass1.toUpperCase() == strNewPass1){
>                //alert('New password must contain at least one lower-case
>character.');
>                errMessage = errMessage + '\n' + 'New password must contain
>at least one lower-case character.';
>                returnValue = false;
>             }
>
>
>             //**********************************************************
>             //  Rule:  password does not match the user name
>             //**********************************************************
>             if (strNewPass1 == strUID){
>                alert('New password may not be the same as your user ID.');
>                returnValue = false;
>             }
>
>
>             //**********************************************************
>             //  Rule:  new password is the same in both password boxes
>             //**********************************************************
>             if (strNewPass1 != strNewPass2){
>                alert('New passwords do not match.');
>                returnValue = false;
>             }
>
>             //**********************************************************
>             //  Rule:  password is not the same as the old
>             //**********************************************************
>             if (strNewPass1 == strOldPass){
>                alert('New passwords may not be the same as the old one.');
>                returnValue = false;
>             }
>
>
>             //**********************************************************
>             //  Rule:  password is not blank
>             //**********************************************************
>             if (strNewPass1 == ''){
>                alert('New password may not be left blank.');
>                returnValue = false;
>             }
>
>
>             //**********************************************************
>             //  Rule:  password meets minimum length requirements
>             //**********************************************************
>             if (strNewPass1.length < minLength){
>                alert('New password may not less than ' + minLength + '
>characters long.');
>                returnValue = false;
>             }
>
>             //**********************************************************
>             //  Rule:  password does not contain the name of a Campbell
>brand
>             //**********************************************************
>             if
>(strNewPass1.match(/((arnotts|campbell|franco|godiva|homepride|pace|prego|stockpot|swanson|v-?8|pepperidge|goldfish))/)){
>                alert('New password may not contain the name of a Campbell
>brand.');
>                returnValue = false;
>             }
>
>
>             //**********************************************************
>             //  Rule:  password contains at least one letter
>             //**********************************************************
>             if (strNewPass1.match(/^[0-9]+$/)){
>                alert('New password must contain both letters and
>numbers.');
>                returnValue = false;
>             }
>
>             //**********************************************************
>             //  Rule:  password contains at least one digit
>             //**********************************************************
>             if (strNewPass1.match(/^[a-zA-Z]+$/)){
>                //alert('New password must contain both letters and
>numbers.');
>                errMessage = errMessage + '\n' + 'New password must contain
>both letters and numbers.';
>                returnValue = false;
>             }
>
>             //**********************************************************
>             //  Rule:  password contains no spaces, tabs, etc
>             //**********************************************************
>             if (strNewPass1.match(/\s/)){
>                //alert('New password may not contain spaces.');
>                errMessage = errMessage + '\n' + 'New password may not
>contain spaces.';
>                returnValue = false;
>             }
>
>
>             if (returnValue == false){ alert(errMessage); }
>
>             //if this returns false to the onSubmit() function within
>             //the <form> tag, then the form submission will be cancelled
>             return returnValue;
>
>          }
>
>
>
>
>
>**********************************************************************
>This e-mail and any files transmitted with it may contain
>confidential information and is intended solely for use by
>the individual to whom it is addressed.  If you received
>this e-mail in error, please notify the sender, do not
>disclose its contents to others and delete it from your
>system.
>
>**********************************************************************
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list