表单验证允许空格

时间:2013-08-21 05:45:41

标签: php jquery validation

在CodeIgniter中,用于名字姓氏和我使用的所有验证。它只需要没有间距的字母。但我需要带空格的信:

HTML

 <p><label for="FIRST_NAME">First Name</label> <input tabindex="1" size="40" type="text" name="FIRST_NAME" id="FIRST_NAME"/></p> 
 <p><label for="LAST_NAME">Last Name</label> <input tabindex="1" size="40" type="text" name="LAST_NAME" id="LAST_NAME"/></p>

的jQuery

function submitForm(){ 

    jQuery.validator.addMethod("lettersonly", function(value, element) {
        return this.optional(element) || /^[a-z]+$/i.test(value);
    }, "Letters only please");

    var validator = $("#myForm").validate({
        rules: { 
            FIRST_NAME: {
                lettersonly: true,
                required:true
            },
            LAST_NAME: {
                lettersonly: true,
                required:true
    }

验证工作正常,我只需要在字段中使用空格进行验证。

2 个答案:

答案 0 :(得分:3)

只需为正则表达式添加空格

return this.optional(element) || /^[a-z ]+$/i.test(value);
                                       ^

答案 1 :(得分:2)

在验证器中为正则表达式添加空格:

jQuery.validator.addMethod("lettersonly", function(value, element) {
    return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters only please");
相关问题