[Javascript] Looking for simple validation script

Esther_Strom at hmco.com Esther_Strom at hmco.com
Wed Feb 6 14:57:41 CST 2002


There are probably more elegant ways to do this, but I have a group of
functions that I wrote years ago that work (there are a few functions in
here that you may not need; this series was designed specifically for
passwords, so there's a function to make sure that the password and verify
password fields are the same; as well as a function to make sure that all
fields are filled. Use whatever combination is required in the
validatePassword function at the top :

var isbad=true;
var isbad1=true;
//==================================================

//"Wrapper" validator function
function validatePassword(form){

if(!allFilled()) //all fields filled in?
            {return false}


if(!checkLength()) //correct length?
            {return false}

if(!letterNumber()) //at least one letter and one number?
            {return false}

if(!newVerifySame()) //new password any verify password the same?
            {return false}

return true;
}

//==================================================

//makes sure all fields are filled
function allFilled(){
      if(document.frmPwd.OldPassword.value == "" ||
            document.frmPwd.NewPassword.value == "" ||
            document.frmPwd.VerifyPassword.value == "") {
                  alert("Please fill in all THREE fields to change your
information.");
            return false;
            }
      else{
      //alert("OK1")
      }
return true
}

//==================================================

//makes sure new password is the correct length
function checkLength(){
var pWord = document.frmPwd.NewPassword.value;
//alert("The length of New Password is " + pWord.length)
      if(pWord.length<6 || pWord.length>8){
            alert("Your password length must be at least 6, but less than
9. (i.e. abc2xyz)");
            document.frmPwd.NewPassword.value="";
            document.frmPwd.VerifyPassword.value="";
            return false;
      }
      else{
      //alert("OK2")
      }
return true
}

//==================================================

//makes sure new password and verify password are the same
function newVerifySame(){
      if(document.frmPwd.NewPassword.value !=
document.frmPwd.VerifyPassword.value){
            alert("Your NEW PASSWORD field must be the same as the VERIFY
PASSWORD field.");
            document.frmPwd.NewPassword.value="";
            document.frmPwd.VerifyPassword.value="";
            return false;
            }
      else{
      //alert("OK3")
      }
return true
}

//==================================================

//checks for letters
function anyLetters(){
      var str=document.frmPwd.NewPassword.value;
      var letters=new Array
("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
      //alert("about to loop");
      str=str.toString();
      str=str.toLowerCase();
      for(i=0;i<=str.length;i++){
            //alert(str.substring(i,i+1));
            for(j=0;j<=letters.length;j++){
                  if(str.substring(i,i+1)==letters[j]){
                        isbad=false;
                  }
             }
      }
      //alert("isbad " + isbad);
      return;
}

//==================================================

//checks for numbers
function anyNumbers(){
      var str=document.frmPwd.NewPassword.value;
      //alert(str);
      var numbers=new Array("1","2","3","4","5","6","7","8","9","0");
      //str=str.toString();
      //var isbad1=true;
      //alert("about to loop");
      for(i=0;i<=str.length;i++){
            //alert(str.substring(i,i+1));
            for(j=0;j<=numbers.length;j++){
                  if(str.substring(i,i+1)==numbers[j]){
                  isbad1=false;
                  }
            }

      }
      //alert(isbad1);
      return;
}

//try this one next time
/*
iif (isNaN(str.replace(/[a-zA-Z]\,\.\#/gi,'')) ==  true) {
      // this means there were no numbers in 'str', so it's likely not an
address
}

*/
//==================================================

//checks for letters & numbers
function letterNumber(){
anyNumbers();
      if(isbad1==true){
            alert("Your password must contain at least one number and one
letter (no number).");
            document.frmPwd.NewPassword.value="";
            document.frmPwd.VerifyPassword.value="";
                  return false;
            }

anyLetters();
      if(isbad==true){
            alert("Your password must contain at least one number and one
letter (no letter).");
            document.frmPwd.NewPassword.value="";
            document.frmPwd.VerifyPassword.value="";
                  return false;
            }

//alert("OK");
return true;

}


//==================================================

//checks for all numbers

function allNums(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only characters and numbers are accepted!");
field.focus();
field.select();
   }
}



                                                                                                                                       
                      Pete Holsberg                                                                                                    
                      <pjh at mccc.edu>           To:      "'Javascript at LaTech.edu'" <javascript at LaTech.edu>                              
                      Sent by:                 cc:                                                                                     
                      javascript-admin         Subject: [Javascript] Looking for simple validation script                              
                      @LaTech.edu                                                                                                      
                                                                                                                                       
                                                                                                                                       
                      02/06/02 02:45                                                                                                   
                      PM                                                                                                               
                      Please respond                                                                                                   
                      to javascript                                                                                                    
                                                                                                                                       
                                                                                                                                       




I need a simple script that will check two input boxes when
"submit" is clicked on.

One input is at least 4 letters and/or digits.

The other is at least 6 characters, that must include
letters and at least one digit.

Suitable "error" messages should appear that explain what
was wrong and bring the person back to the form.

Is there any thing simiar posted somewhere?

Thanks,
Pete

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript







More information about the Javascript mailing list