Reputation: 360
Im using jquery validate and I wish to add a method that input text cannot contain ".com"
Im tried:
//.com Validator
$.validator.addMethod('dotcom', function(value, element) {
return this.optional(element) || (!value.match(/[.com]/) && value.match(/[.com]/));
},
dotcom);
But this doesn't work, any suggestion to do that ? thanks
Upvotes: 0
Views: 823
Reputation: 388316
It looks like your regex test is wrong. Try
var dotcom = 'Value cannot contain `.com`'
$.validator.addMethod('dotcom', function(value, element) {
return this.optional(element) || !/\.com/.test(value);
}, dotcom);
$('form').validate({
rules: {
site: {
dotcom: true
}
}
})
$('button').click(function() {
$('form').valid();
return false;
})
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form>
<div>
<input name="site" />
</div>
<button>Save</button>
</form>
Upvotes: 1