jquery验证不仅适用于正则表达式

时间:2014-07-02 09:12:55

标签: jquery regex validation

每当我验证代码,说明,localisedDescription.my validate.addmethod都没有正常工作。请你尽快给我一个解决方案,以便我的正则表达式能正常工作。这些是代码,描述,localisedDescription的验证声明。 / p>

Fields & Actions
1.  Code
a.  Length - 20
b.  Regular Expression – alphabets+ digit
i.  No special characters + No spaces + No dashes
c.  Default value:  Blank
d.  Mandatory field
e.  Validation
i.  On Submit (Not On-field tab)
ii. Label Highlighted in Bold + Red
iii.    Text field – Text highlighted for Invalid data
iv. Validation message on top of the screen
v.  For Blank data– Message:-  ‘Code is a required field. It must consist of Roman Letters/numbers. Maximum length should not exceed 20 characters. ‘
vi. Invalid data – Message:- ‘Code is a required field. It must consist of Roman Letters/numbers. Maximum length should not exceed 20 characters.
2.  Description
a.  Length – 80
b.  Regular Expression – alphabets+ digit+ dashes + spaces + apostrophe
c.  Default value:  Blank
d.  Mandatory field
e.  Validation
i.  On Submit (Not On-field tab)
ii. Label Highlighted in Bold + Red
iii.    Text field – Text highlighted
iv. Validation message on top of the screen
v.  Blank – Message:- ‘English Description is a required field. It must consist of Roman letters/numbers, spaces, apostrophes and dashes and Maximum length should not exceed 80 characters.’
vi. Invalid data – Message:- ‘English Description is a required field. It must consist of Roman letters/numbers, spaces, apostrophes and dashes and Maximum length should not exceed 80 characters
3.  Arabic Description
a.  Length – 80
b.  Regular Expression – Arabic characters+ digits + dashes + spaces 
c.  Default value:  Blank
d.  Mandatory field
e.  Validation
i.  On Submit (Not On-field tab)
ii. Label Highlighted in Bold + Red
iii.    Text field – Text highlighted
iv. Validation message on top of the screen
v.  Blank – Message:- ‘Arabic Description is a required field. It must consist of Arabic characters/numbers, spaces and dashes and Maximum length should not exceed 80 characters’
vi. Invalid data – Message:- ‘Arabic Description is a required field. It must consist of Arabic characters/numbers, spaces and dashes. Maximum length should not exceed 80 characters’`enter code here`



$.validator.addMethod("adminCodeCheck", function(value) {
    alert("hello");
        return (/^[A-Za-z0-9]{0,20}$/.test(value));
        });

    $.validator.addMethod("adminDescriptionCheck", function(value) {
        return (/^[a-zA-Z][a-zA-Z0-9 \-']{0,80}$/).test(value);
    });

    $.validator.addMethod("adminArabicDescriptionCheck", function(value) {
        return (/^[a-zA-Z][a-zA-Z0-9 \-]{0,80}$/).test(value);
    });


        function validateCarrier(formId, errorcontainerId){

        return $(formId).validate({     
            rules: {
                    code: {             
                        required: {
                            depends:function(){
                            alert("hello2");
                                $(this).val($.trim($(this).val()));
                                return true;

                            }
                        }                           
                        //adminCodeCheck: true              

                    },
                    description: {                      
                        required: {
                            depends:function(){
                                $(this).val($.trim($(this).val()));
                                return true;
                            }
                        }                               
                    //  adminDescriptionCheck: true     
                    },


                    localisedDescription: {                     
                        required: {
                            depends:function(){
                                $(this).val($.trim($(this).val()));
                                return true;
                            }
                        }                               
                    //  adminArabicDescriptionCheck: true       
                    }
                },
            messages: {         
                code: {

                    required: AdminCodeRequired
                    //adminCodeCheck:AdminCodeRequired

                },
                description: {
                    required: AdminDescriptionRequired
                    //adminDescriptionCheck: AdminDescriptionRequired
                },
                localisedDescription: {
                    required: AdminArabicDescriptionRequired
                    //adminArabicDescriptionCheck:AdminArabicDescriptionRequired
                }

            },
            errorContainer: errorcontainerId,
            errorLabelContainer: errorcontainerId+"ul",
            wrapper: "li"
        });
        }

        function highlightcarrier(validator){
        var isError =false;
            if(!validator.element("#code")){
                $("#code").addClass("errorclass");          
                isError = true;
            } else {
                $("#code").removeClass("errorclass");
                isError = true;
            }

            if(!validator.element("#description")){
                $("#description").addClass("errorclass");           
                isError = true;
            } else {
                $("#description").removeClass("errorclass");
                isError = true;
            }
            if(!validator.element("#localisedDescription")){
                $("#localisedDescription").addClass("errorclass");      
                isError = true;
            } else {
                $("#localisedDescription").removeClass("errorclass");
                isError = true;
            }



            return isError;
    }

1 个答案:

答案 0 :(得分:0)

代码"代码":/^[A-Za-z0-9]{0,20}$/

对于"说明":/^[A-Za-z0-9\s'-]{0,80}$/

对于" localisedDescription"你必须使用一个插件。例如,您可以使用XRegExp(在此处找到:http://xregexp.com/plugins/)来创建此模式:XRegExp("^[\\p{Arabic}\s'-]{0,80}$")