Reputation: 289
I have a regular expression for "media_topic" it should allow characters and space and also it should allow empty data because it is not mandatory field. When I submit without entering anything means it should to allow submit but it is happening.
Below is my code,
$(document).ready(function(){
$.validator.addMethod('media_topic', function(value, element) {
var regex = new RegExp("^[a-zA-Z][a-zA-Z0-9,_-\\s]*$");
var key = value;
if (!regex.test(key)) {
return false;
}
return true;
}, "Please valid media topic.");
Upvotes: 0
Views: 39
Reputation: 46
May be it is better to check first if your value is empty before performing regex.
e.g.
if (value == "") {return True;}
Upvotes: 0
Reputation: 1573
try adding
ignore: ".ignore"
When field is empty add class .ignore to it if not empty then remove that class
Hope this Helps.
Upvotes: 0
Reputation: 521249
Try using an alternation in your regex which allows for no input:
var regex = new RegExp("^[a-zA-Z][a-zA-Z0-9,_\\s-]*|$");
The basic idea is:
^(some pattern)|$
Upvotes: 1