This exact question was asked here:
using the jquery validation plugin, how can I add regular expression validation to a text box?
But unfortunately, the solution does not work for me. I use the very popular jquery validation plugin, in particular this version:
http://jquery.bassistance.de/validate/jquery.validate.js
I tried to add a custom alphanumeric function (from the above stack overflow question):
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/validate/jquery.validate.js"></script>
<script type="text/javascript">
$(function() {
$.validator.addMethod("loginRegex", function(value, element) {
return this.optional(element) || /^[a-z0-9\-]+$/i.test(value);
}, "Username must contain only letters, numbers, or dashes.");
$("#myForm").validate({
rules: {
"login": {
required: true,
loginRegex: true,
}
},
messages: {
"login": {
required: "You must enter a login name",
loginRegex: "Login format not valid"
}
}
});
});
</script>
<form method="post" id="myForm">
<input type="text" name="login" />
<input type="submit" value="Go" />
</form>
If you run this code, you will see that it only checks for the presence of some type of input, it does not check the input through a custom alphanumeric function. How can this be made to work?
source
share