I have a pre-defined settings to built password and those are
- Minimum length
- Using Alphabets (true or false)
- Using Numeric (true or false)
- Using Case Characters
- Using Alphabets (allow only ,‘#&-_“.)
I created a java-script function to build the same
function BuildRegex(alphabets, numerics, cases, specials) {
var pattern = "^[";
if (alphabets == "True")
pattern += "a-z";
if (cases == "True")
pattern += "A-Z";
if (numerics == "True")
pattern += "0-9";
if (specials == "True")
pattern += ",‘#&-_“.";
pattern += "]*$";
return pattern;
}
and I used the pattern as:
var re = new RegExp(pattern);
if (!re.test(input)) {
$("#lblError").show();
}
But it is not working :(
Help needed!!
Sample code :
<html>
<head>
Add Jquery CDN here
<script type="text/javascript">
function BuildRegex(alphabets, numerics, cases, specials) {
var pattern = "^[";
if (alphabets == "True")
pattern += "a-z";
if (cases == "True")
pattern += "A-Z";
if (numerics == "True")
pattern += "0-9";
if (specials == "True")
pattern += ",‘#&-_“.";
pattern += "]*$";
return pattern;
}
$(document).on("change", "#txtPassword", function () {
debugger;
var input = $(this).val();
if ($.trim(input) != "") {
var pattern = BuildRegex("True", "False", "True", "True");
var re = new RegExp(pattern);
if (!re.test(input)) {
$("#lblError").show();
}
else {
$("#lblError").hide();
}
}
else {
$("#lblError").hide();
}
});
</script>
</head>
<body>
<input type="text" id="txtPassword" />
<span id="lblError" style="display:none;">Invalid characters in password.</span>
</body>
Comments
Post a Comment