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">
&#13;
答案 0 :(得分:1)
如果你把它分成几部分,你可以很容易地做到这一点,并告诉用户他们确实失败了哪个约束,如下所示:
// 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!
答案 1 :(得分:0)
测试是否满足所有条件(c1
条件1等)
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
答案 2 :(得分:0)
您可以创建一个RegExp
对象,然后将密码传递给其test
方法。 RegExp
对象可以使用正向前瞻来断言它找到一个大写字符和一个数字字符。
最后,您可以通过尝试模式匹配字符串开头标记(^)和结尾之间的至少8个非空白字符来测试密码长度至少为8个字符并且不包含空格字符-of-string标记($)。模式匹配将使用整个字符串,因此如果找到任何空白字符,测试将失败,如果少于8个字符,测试也将失败。
/(?=.*[A-Z])(?=.*\d)^\S{8,}$/.test(password)
^uppercase ^digit ^8+ characters in length and no whitespace
如果密码正常,则此表达式将评估为true,否则为false。