Welcome to PHP Smarty Q&A, where you can ask programmaing questions and receive answers from other members of the community for PHP, Smarty, MYSQL related. You can also share or save your scripts in this repository for future reference.

jQuery Validate Custom Rule for minlength

+1 vote
1,728 views

I am using other custom validators and hence I cannot use default minlength rule as my site has multilingual. is there a way where I can add a custom rule similar to the following:

    $.validator.addMethod("pwchecklowercase", function (value) {
        return /[a-z]/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter.");

Kindly help.

By the way, I am using jquery.validate.min.js version 1.11

Thanks in advance.

asked Oct 18, 2015 in JQuery by seeker
What is the purpose of this, you just want to change the message?
Yes, exactly. As I said, I am using the multilingual site and hence I need it.

1 Answer

0 votes
 
Best answer

Very well, there is a very good reference site for this. http://johnnycode.com/2014/03/27/using-jquery-validate-plugin-html5-data-attribute-rules/

In case, if site goes inaccessible, am pasting some of the examples here.

Using the data attributes, you can do lot more stuff as you usually do with basic stuff, here it goes.

 

<form id="validate-me-plz">
    <div>
        Required: <input type="text" name="required" data-rule-required="true" />
    </div>
    <div>
        Required w/custom message: <input type="text" name="required-sassy" data-rule-required="true" data-msg-required="Please enter SOMETHING." />
    </div>
    <div>
        Email: <input type="text" name="email" data-rule-email="true"/>
    </div>
    <div>
        Email w/custom message: <input type="text" name="anotherEmail" data-rule-email="true" data-msg-email="Please enter a valid email address you dummy." />
    </div>
    <div>
        <input type="submit" value="Validate!" />
    </div>
</form>

// javascript call
$('#validate-me-plz').validate();

 

Data Attributes examples:

Rule List:

(Tested, core)

    data-rule-required="true"
    data-rule-email="true"

(Untested, core, but should work)

    data-rule-url="true"
    data-rule-date="true"
    data-rule-dateISO="true"
    data-rule-number="true"
    data-rule-digits="true"
    data-rule-creditcard="true"
    data-rule-minlength="6"
    data-rule-maxlength="24"
    data-rule-rangelength="5,10"
    data-rule-min="5"
    data-rule-max="10"
    data-rule-range="5,10"
    data-rule-equalto="#password"
    data-rule-remote="custom-validatation-endpoint.aspx"

(Untested, additional, but should work)

    data-rule-accept=""
    data-rule-bankaccountNL="true"
    data-rule-bankorgiroaccountNL="true"
    data-rule-bic=""
    data-rule-cifES=""
    data-rule-creditcardtypes=""
    data-rule-currency=""
    data-rule-dateITA=""
    data-rule-dateNL=""
    data-rule-extension=""
    data-rule-giroaccountNL=""
    data-rule-iban=""
    data-rule-integer="true"
    data-rule-ipv4="true"
    data-rule-ipv6="true"
    data-rule-mobileNL=""
    data-rule-mobileUK=""
    data-rule-lettersonly="true"
    data-rule-nieES=""
    data-rule-nifES=""
    data-rule-nowhitespace="true"
    data-rule-pattern=""
    data-rule-phoneNL="true"
    data-rule-phoneUK="true"
    data-rule-phoneUS="true"
    data-rule-phonesUK="true"
    data-rule-postalcodeNL="true"
    data-rule-postcodeUK="true"
    data-rule-require_from_group=""
    data-rule-skip_or_fill_minimum=""
    data-rule-strippedminlength=""
    data-rule-time=""
    data-rule-time12h=""
    data-rule-url2=""
    data-rule-vinUS=""
    data-rule-zipcodeUS="true"
    data-rule-ziprange=""

If some of them does not work, try including additional-methods.js after jquery.validate.min.js

Hope you will enjoy with this attributes.

answered Oct 18, 2015 by robert (13,210 points)
selected Oct 18, 2015 by robert
Whoa!!! I never knew this. This is awesome. Thank you so much for the reference. I tried on jsfiddle with given example, it worked beautifully, thank you.
...