Validation controls jquery Validation engine call external function validation

Source: Internet
Author: User

When using the jquery Validation engine, We can customize the regular validation ourselves in addition to our own Api. Custom regular validation The previous article has been told, what if you want to use a custom function for validation? In fact, this control has a bug, in the API said

This is where we need to verify the Funccall[custom function name], but we will find that there is always an error, saying that you can not find the function Name. In fact, it is required to add required for comprehensive verification, that is, validate[required,funccall[yorn]], so that it will recognize our custom Function. But we just don't want to required this? Or do we want to make required this non-null verification function in accordance with certain conditions?

<textarea class= "m-area add-tw validate[funccall[yorn"] "id=" remark "cols=" "rows=" ></textarea>

Then you need to use the following methods of comprehensive implementation, online Foreigner's solution is to push a required in the function, you can let it execute normally.

I've defined a Yorn function, and the basic format is functions Yorn (field, rules, i, options) {}, where field represents the element you're validating, and I'm validating textarea here, Then field represents the Textarea.

First of all, i'll talk about the features I want to implement, and my requirement is to trigger the verification when I click on the Non-pass and the memo is empty, prompting "must fill in the content". The verification prompt cannot be triggered if the comment is empty, the comment is not empty, and the comment is not Empty. If validate[required] this way, the prompt message will be triggered as long as the memo message is not filled IN. So my idea is that when you click on the Save button to determine, click on which radio, click Do not pass when the radio add a custom attribute, while assigning Value. and delete this custom attribute when clicking through Radio. Then go into the validation, that is, enter the custom function, get to the value of the custom property not through the radio, get to the note value, and then determine whether the two conditions are also established, if the condition is established, then first Rules.push (' required '); Because we said Earlier. if you want to implement a custom function validation, you must have required, then return to options.allrules.required.alertText; What do you mean by this sentence? is to return the information you want to Prompt. This message is also set Elsewhere.


HTML file, validate[funccall[yorn]] [set Custom Function Validate[funccall[custom function name]]
 <form id= "approval" >//this validation control must have a form <div class= "name-ipt" > <div class= "m-nam E "><span> views:</span></div> <input type=" Radio "name=" trial "class=" M-radio Col "/>            Through <input type= "radio" name= "trial" class= "m-radio col" id= "nocheck"/> not through </div> <div class= "name-ipt" ></div> <div class= "area-ipt add-td" > <div C lass= "m-name" ><span> remarks:</span></div> <textarea class= "m-area add-tw Validate[funcca ll[yorn]] "id=" remark "cols=" "rows=" ></textarea> <span class= "m-span add-stl" > remaining can be entered 100 0 words </span> </div> </div></form><div class= "add-sb" > <a href= "#" clas s= "save-btn m" ng-click= "saveaudit ()" > Save </a></div> 
(valid: step 1)

Because I am using the angular, so to save an event, click on save to trigger the event, and then determine the two radio which was checked, the other said Above. The validation is then triggered to enter the custom Function. 、、

Own JS file written in
Save $scope. saveaudit = function () { if (' #nocheck ') {$ (' checked ')) { $ ("#remark"). attr ("trialattr" , ' N '); } else{ $ ("#remark"). removeattr (' trialattr '); } If ($ (' #approval '). validationengine (' validate ')) {//verify What to do after passing the word}}
(valid: Step 2)

The custom function must take Rules.push (' required ') and return options.allrules.required.alertText; This custom function is written in its own JS File.

Your own JS file is written in
function Yorn (field, rules, i, options) { var trialattr = field.attr ("trialattr"); var textareaval = Field.val (); if (trialattr = = "N" && textareaval== ") { rules.push (' required '); return options.allrules.required.alertText;} }
(valid: Step 3)

next, to set the custom prompt information, find the jquery.validationengine-zh_cn.js file, and then at the bottom of your own following an object, function name in front, followed by Alerttext followed by the information you want to Prompt. This will be Ok.

This is written in Jquery.validationengine-zh_cn.js.

(valid: Step 4)

I previously defined a function in the following API under this file, but found that it could not be executed at all, because there is no required in the custom function that first determines validate[required,funccall[yorn]] when entering Validation. If it does not exist, you cannot enter into jquery.validationengine-zh_cn.js this file to execute our internal defined Function. however, If you put the custom function in our own js, we will first execute our own JS file function, and finally determine whether there is required, and this time we have push in, so there will be no error to say that you can not find your custom function Name.

(this is invalid and unenforceable, do not Use)

This validation control is actually quite good, but this bug to engage in a half-day, fortunately there is a solution on the internet, but seemingly no one like I said so clearly, are pointed out just a bit. Hope to be able to help everyone, at the same time to retain their own later Use.

Validation controls jquery Validation engine call external function validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.