ISHAN MAHAJAN ISHAN MAHAJAN - 3 months ago 10
Javascript Question

How to add proper validation for password and confirm password input?

I am using this script don't know how it works but it makes neccessary for a user to use a capital letter and a lower case alphabet



<input title="Password must contain at least 6 characters, including UPPER/lowercase and numbers" type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}" maxlength="20" name="password" onchange="
this.setCustomValidity(this.validity.patternMismatch ? this.title : '');
if(this.checkValidity()) form.confirm_password.pattern = this.value;
" placeholder="Password"/>
<input title="Please enter the same Password as above" type="password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}" name="confirm_password" onchange="
this.setCustomValidity(this.validity.patternMismatch ? this.title : '');" placeholder="Confirm Password" maxlength="20" id="confirm_password">





And I wanted that user can either input a speacial character or a Capital letter so I replaced
[A-Z]
with
[A-Z~@#$^*()_+=[\]{}|\\,.?: -]
but it is still expecting a Capital letter and a lowercase letter.
My question is how to allow user to input either a Speacial character or A Capital letter.

In my example if user wants to submit a Speacial character he is not allowed to do so how do I overcome that.

Answer

You can go with this:

<form name="form1" action="#">  
<ul>  
<li><input type="password" name="pwd1"/></li>  
<li>&nbsp;</li>  
<li class="submit"><input type="submit" name="submit" value="Submit" onclick="CheckPassword(document.form1.pwd1)"/></li>  
<li>&nbsp;</li>  
</ul>  
</form>

Javascript:

function CheckPassword(inputtxt)   
{   
   var passw=  /^[A-Za-z]\w{7,14}$/;  
   if(inputtxt.value.match(passw))   
   {   
      alert('Correct, try another...')  
      return true;  
   }  
   else  
   {   
      alert('Wrong...!')  
      return false;  
   }  
}