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-name" ><span> comments:</span></div> <input type= "Radio" name= "Trial" class= "M-radio col"/> Through <input type= "Radio" name= "trial" CLA ss= "M-radio Col"id= "Nocheck"/> does not pass </div> <div class= "Name-ipt" ></div> <div class= "Area-ipt ad D-td "> <div class=" m-name "><span> remarks:</span></div> <textarea cl ass= "M-area ADD-TWValidate[funccall[yorn]]"id=" remark "cols=" "rows=" ></textarea> <span class= "M-span add-stl" > remaining can input 1000 words </sp An> </div> </div></form><div class= "ADD-SB" > <a href= "#" class= "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 function () {if (' #nocheck '). attr (' 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.