我把一個函數放在一起,幫助您獲得需要使用密碼規則的位置。由於我不確定您如何計劃實現應用程序的這一部分,因此我將留給您以在表單提交中實現它。
更新:我添加了代碼,在檢查規則條件之前從輸入字符串中刪除空格。
原因:如果在輸入中允許空格,則空格將作爲數字檢查的一部分進行計數並返回true。
的jsfiddle Validate Input with Special Rules
測試HTML:
<div id="results"></div>
測試JavaScript:
/*
The rules to follow are:
The length of code must be 27. Not less, not more.
AA BB C DDDDD EEEEE FFFFFFFFFFFF
AA must be "IT" (uppercase). first 2 characters
BB are numbers (even with the initial zero). 3 & 4 characters
C is a character (uppercase). 5th character
DDDDD are numbers (even with the initial zero). characters 5 - 10
EEEEE are numbers (even with the initial zero). characters 11 - 15
FFFFFFFFFFFF are characters and numbers. It's sufficient that the length is 12.
characters 16 - 27 above
*/
var input = "IT12C1234567891FFFFFFFFFFFF";
validate(input);
function validate(input){//begin function
//remove all of the white space from the input
var input = input.replace(/\s+/g, '');
//test for length
var testLength = input.length === 27;
//2nd and 3rd characters equal IT
var AA = input.substring(0,2) === "IT";
//3rd and 4th characters are numbers
var BB = !isNaN(input.substring(2,4));
//The 5th character is a non numerical capitalized character
var C = isNaN(input.split("")[4]) && input.split("")[4] === input.split("")[4].toUpperCase();
//characters 5 - 10 are numbers
var DDDDD = !isNaN(input.substring(5,10));
//characters 11- 15 are numbers
var EEEEE = !isNaN(input.substring(10,15));
//characters 16 - 27 can be characters and numbers as long as the length is 12
var FFFFFFFFFFFF = input.substring(15,27).length === 12;
//if all the password rules checks return true
if(testLength && AA && BB && C & DDDDD && EEEEE & FFFFFFFFFFFF){//begin if then
//do what you need to do here if the password is valid
}
else{
//let the user know the error here
}
//display test results
document.getElementById("results").innerHTML = testLength + "<br>"
+ AA + "<br>"
+ BB + "<br>"
+ C + "<br>"
+ DDDDD + "<br>"
+ EEEEE + "<br>"
+ FFFFFFFFFFFF + "<br>";
}//end function
除非你的問題是專門關於這個插件,請不要使用jQuery的驗證標籤。編輯。謝謝。 – Sparky