2011-12-19 11 views
1

jqueryでフォーム内の1つの入力フィールドを検証する関数を作成しました。私のフォームにはさらに2つのテキストフィールドがあります。私は彼らに追加​​のルールを追加する方法がわからない。これは私がこれまでに試したことです。JQueryでの入力フィールドの検証

$(document).ready(function() { 
$(document).ready(function() { 
jQuery.validator.addMethod("url_validation", function (value, element) { 
return this.optional(element) || /^(([http|https|HTTPS|HTTP]+:\/\/))?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)[email protected])?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/i.test(value); 
}, "error"); 

$('#myform').validate({ 
    /** this section is working */ 
    rules: { 
     text_name: { 
      required: true, 
      url_validation: true, 
      maxlength: 2000 
     } 
    }, 
    messages: { 
     text_name: { 
      required: "Please specify a URL", 
      url_validation: "The domain you have entered is not a valid domain or sub domain name. Please try again.", 
      maxlength: "You have exceeded the maximum length" 
     } 
    } 
    /**********************************************************/ 
    /*this is where i tried to add other rules to two of my other textboxes*/ 
    rules: { 
     text_name2: { 
      required: true, 
      url_validation: true, 
      maxlength: 2000 
     } 
    }, 
    messages: { 
     text_name2: { 
      required: "Please specify a URL", 
      url_validation: "The domain you have entered is not a valid domain or sub domain name. Please try again.", 
      maxlength: "You have exceeded the maximum length" 
     } 
    } 
    /*above thing is not working*/ 

}); 

私のHTMLには、検証する必要がある他の2つのテキストボックスがあります。どのようにこのタスクを実行するには?

答えて

2

validateプラグインは、rulesmessagesというオプションオブジェクトをパラメータとして取ります。それらを埋めるが、rulesmessagesオブジェクトを二重に指定しないでください。

それは次のようになります。

$('#myform').validate({ 
    rules: { 
     text_name: { 
      ... 
     }, 
     text_name2: { 
      ... 
     } 
    }, 
    messages: { 
     text_name: { 
      ... 
     }, 
     text_name2: { 
      ... 
     } 
    } 
}); 
関連する問題