2011-12-19 5 views
1

jquery로 양식의 한 입력 필드의 유효성을 검사하는 함수를 작성했습니다. 내 양식에는 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

유효성 검사 플러그인은 rulesmessages을 매개 변수로 사용하는 옵션 객체를 사용합니다. 그 (것)들을 위로 채우십시오, 그러나 rulesmessages 목표를 지정하십시오.

이 있어야한다 :

$('#myform').validate({ 
    rules: { 
     text_name: { 
      ... 
     }, 
     text_name2: { 
      ... 
     } 
    }, 
    messages: { 
     text_name: { 
      ... 
     }, 
     text_name2: { 
      ... 
     } 
    } 
});