简体   繁体   中英

Jquery Validate if field contains a comma

I have been looking around but can't find a way to do this. I really like the jquery validate script and I was wondering if there was a way to check to see if a field contains a comma and not validate if there is no comma.

您可以按此处所述使用.inArray()方法。应拆分字符串,然后可以使用此方法: var found = $.inArray(',', categories) if(found == -1) alert("Error!")

not familiar with jquery validate but str.indexOf returns the position where something appears in a string, -1 means it doesnt. check a string for a comma like this :

if (yourString.indexOf(',') > -1) {
    validate();
}

If you are referring to the jquery validate plugin:

$.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "Please check your input."
);

$("#input").rules("add", { regex: "," })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM