Simple JQuery Validator addMethod not working
- by tehaaron
Updated question on the bottom
I am trying to validate a super simple form. Eventually the username will be compared to a RegExp statement and the same will go for the password. However right now I am just trying to learn the Validator addMethod format.
I currently have this script:
JQuery.validator.addMethod(
"legalName",
function(value, element) {
if (element.value == "bob")
{
return false;
}
else return true;
},
"Use a valid username."
);
$(document).ready(function() {
$("#form1").validate({
rules: {
username: {
legalName: true
}
},
});
});
Which if I am not mistaken should return false and respond with "Use a valid username." if I were to put "bob" into the form.
However, it is simply submitting it.
I am linking to JQuery BEFORE Validator in the header like instructed.
My uber simple form looks like this:
<form id="form1" method="post" action="">
<div class="form-row"><span class="label">Username *</span><input type="text" name="username" /></div>
<div class="form-row"><input class="submit" type="submit" value="Submit"></div>
</form>
Finally how would I go about restructing the addMethod function to return true if and false at the else stage while keeping the message alert for a false return?
(ignore this last part if you don't understand what I was trying to say :) )
Thanks in advance.
Thank to everyone who pointed out my JQuery - jQuery typo.
New
Ideally, I am trying to turn this into a simple login form (username/password). It is for demonstration only so it wont have a database attached or anything, just some simple js validations. I am looking to make the username validate for <48 characters, only english letters and numbers, no special characters. I thought a whitelist would be easiest so I had something like this: ^[a-zA-Z0-9]*${1,48} but I am not sure if that is proper JS RegExp (it varies from Ruby RegExp if I am not mistaken?...Usually I use rubular.com). Password will be similar but require some upper/lowercase and numbers.
I believe I need to make another $.validator.addMethod for legalPassword that will look very similar.