Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript validation issue for password

im having some issues concerning my javascript code for password. here's my code .that part is working fine, but i want to put another condition where my password should contains at least 8 characters and must abide the following rules : no spaces, contains at least 1 Uppercase and a number. concerning the mobile, it must always start with the no. 5 . help <3

    function formValidation() {
 var mobile = document.forms["form"]["mobile"].value;
        var password = document.forms["form"]["password"].value;
       
//reg expression check
 var checkNumbers = /^[0-9 ]+$/;


$(document.forms["form"]["mobile"]).focus(function(){
		$(document.forms["form"]["mobile"]).css("background-color", "white");
	});
$(document.forms["form"]["password"]).focus(function(){
		$(document.forms["form"]["password"]).css("background-color", "white");
function clear(){
$(document.forms["form"]["mobile"]).focus(function(){
		$(document.forms["form"]["mobile"]).css("background-color", "white");
	});
$(document.forms["form"]["password"]).focus(function(){
		$(document.forms["form"]["password"]).css("background-color", "white");
	});
}

 if (mobile == null || mobile == "") {
        error[error.length]=("Enter your mobile number"); 
        document.form.mobile.focus();
        $(document.forms["form"]["mobile"]).css("background-color", "blue");  
        ;         
    }else if (mobile != null || mobile != "") {
    	if(!checkNumbers.test(mobile)){
    	error[error.length]=("Enter Only numeric Characters for mobile phone");
		document.form.mobile.focus();
		$(document.forms["form"]["mobile"]).css("background-color", "blue");
    	}
               
    }
        if (password == null || password == "") {
            error[error.length]=("Enter a password"); 
            document.form.password.focus();
            $(document.forms["form"]["password"]).css("background-color", "blue");   
        }

    }
   		
    <form name="form" onsubmit="return formValidation()" action="process.html">
    
Mobile phone:
		<input type="text" name="mobile" id="mobile"></br></br>

Password:
    <input type="password" name="password" id="password"></br></br>
    </form> 
<input id="submit" type="submit" name="submit" id="submit">
like image 203
supergirl Avatar asked Jan 30 '26 02:01

supergirl


1 Answers

If you break it down into parts you can do this really easily and inform the user exactly which constraint they are failing, like this:

// Check the length:
if (password.length < 8) { // ... not long enough }

// Check if it has at least one upper case:
if (password.match(/[A-Z]+/g) === null) { // ... no upper case characters }

// Check if it has at least one number:
if (password.match(/\d/g) === null) { // ... no numbers }

// Password passes validation!
like image 89
jconder Avatar answered Jan 31 '26 17:01

jconder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!