3

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">

3 Answers 3

1

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!
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, I have it working at this jsfiddle (jsfiddle.net/4w6x0311), can you try your inputs there?
0

Test if all your conditions are met (c1 condition 1 and so on)

var c1 = (password.toLowerCase() != password);// if it HAS uppercase letters, it won't match
var c2 = password.length > 8;
var c3 = !(password.indexOf(" ") > -1); // no spaces
var c4 = password.match(/\d+/g); // matches numbers

Comments

0

You can create a RegExp object and then pass in your password to its test method. The RegExp object can use positive lookaheads to assert that it finds an uppercase character and a digit character.

Finally, you can test that the password is at least 8 characters long and contains no white-space characters by attempting to pattern match at least 8 non-whitespace characters between the beginning-of-string token (^) and the end-of-string token ($). The pattern match will consume the entire string so if any whitespace characters are found, the test will fail, and if there are less than 8 characters, the test will also fail.

/(?=.*[A-Z])(?=.*\d)^\S{8,}$/.test(password)
 ^uppercase ^digit  ^8+ characters in length and no whitespace

This expression will evaluate to true if the password is okay and false if not.

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.