0

I've spent many hours trying to work this out. I know it's something easy but it just will not work for me!

I would like to validate for a password using this expression ^.*(?=.{6,})(?=.*[a-zA-Z])[a-zA-Z0-9]+$ with javascript.

I'm not sure how to structure the function and how to call it in the code. I've got something working for validating the email but I can't make the password expression work.

function validateEmail()
{     
   var emailID = document.myForm.email.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email address")
       document.myForm.email.focus() ;
       return false;
   }
   return( true );
}

function validate()
{
    if( document.myForm.email.value == "" )
       {
         alert( "Please provide your Email!" );
         document.myForm.email.focus() ;
         return false;
       }
    else
       {
         // Put extra check for data format
         var ret = validateEmail();
         if( ret == false )
         {
            return false;
       }
}

I would like to call the passwordChecker from the validate function.

1
  • why cant you just call passwordChecker(); like that? Commented Oct 3, 2012 at 11:30

2 Answers 2

1

This should do

function validateEmail()
{     
   var emailID = document.myForm.email.value;
   atpos = emailID.indexOf("@");
   dotpos = emailID.lastIndexOf(".");
   if (atpos < 1 || ( dotpos - atpos < 2 )) 
   {
       alert("Please enter correct email address")
       document.myForm.email.focus() ;
       return false;
   }
   return true;
}

function validatePassword()
{     
   var reg = /^.*(?=.{6,})(?=.*[a-zA-Z])[a-zA-Z0-9]+$/;
   return reg.test(document.myForm.password.value);       
}

function validate()
{
    if(document.myForm.email.value == "" || !validateEmail())
    {
         alert( "Please provide a valid Email!" );
         document.myForm.email.focus() ;
         return false;
    }
    else if(!validatePassword())
    {
         alert("Please provide a valid password!");
         document.myForm.password.focus() ;
         return false;
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much billy! It makes so much more sense :)
0

My suggestion

function isEmail(email) {     
   var re = /^.*(?=.{6,})(?=.*[a-zA-Z])[a-zA-Z0-9]+$/; 
   return re.test(email); 
}

function validate() {
    var email = document.myForm.email;
    if (email.value.trim() =="") { // may need IE support
      alert( "Please provide your Email!" );
      email.focus() ;
      return false;
    }
    if (!isEmail(email.value)) {
      alert( "Please provide a valid Email!" );
      email.focus() ;
      return false;
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.