2

I m using jQuery plugin for inline form validation in my .NET web application.
With following:

  1. jquery.validationEngine.js
  2. jquery.validationEngine-en.js
  3. jquery-1.6.min.js

  4. validationEngine.jquery.css

  5. template.css

Now i have two html <select> , one is for minimum value and second is for maximum value. Values should be check onchange.

How to use custom functions to Validate minimum and maximum values.

4
  • That article makes me extremely sad. First of all...You're storing variables that you only ever use once...why would you do that to yourself. Second, you're using alert's, but you actually want a modal window or dialogue...what have you tried?
    – Ohgodwhy
    Dec 20, 2014 at 6:06
  • Thanks for your quick response. Above code is working perfectly. Messages will be display using Alert(). That's i don't want. I can't use modal window or dialog. Min and Max should display error message like other controls. I request you to plz have a look at aspdotnet-suresh.com/2012/01/… url. Dec 20, 2014 at 6:12
  • I looked at the page. So you need a tooltip to popup or a form helper. Sure..My question still stands... WHAT HAVE YOU TRIED?
    – Ohgodwhy
    Dec 20, 2014 at 6:14
  • inside jquery.validationEngine-en.js , code is: "email": { "regex": regular expression, "alertText": "* Invalid email address" }, i used it in my code: <td><asp:TextBox ID="txtemail" runat="server" CssClass="validate[required,custom[email]]" /> But for min & max, i couldn't use regular expression. I dont' have any idea. Plz tell me what should i do. Dec 20, 2014 at 6:34

2 Answers 2

0

Untested but according to the reference of the jQuery plugin, you can work with custom functions to validate a field value:

HTML:

<input class="validate[required,funcCall[checkMinMax]]" type="text" ...

JavaScript:

function checkMinMax(field, rules, i, options) {
    var minVal = parseInt($("#sel_min").val(), 10),
        maxVal = parseInt($("#sel_max").val(), 10);
    if(!isNaN(minVal) && !isNaN(maxVal)) {
         if (minVal > maxVal) {
            return 'YOUR MESSAGE TEXT HERE';
         } else if (maxVal < minVal) {
            /* OR use the options with */
            return options.allrules.validate2fields.alertText;
         }
    }
}

It's a long time since I saw the usage of another jQuery validation plugin than jQuery validate, comparing both I suggest you should look into this, because it became more a less the 'standard' way to handle form validation with jQuery, it's well documented and you'll find most of the problems already solved.

1
  • Hi axel.michel, Your code helped me to solve out my problem. Thanks for considering my problem. Very-2 grateful to you. Dec 22, 2014 at 3:13
-1
<select id="sel_max" name="sel_max" style="width:83px" class="validate[required,funcCall[checkMinMax]]" > 

jquery.validationEngine.js :

 window.checkMinMax = function(field, rules, i, options) {              
        var minVal = parseInt($("#sel_min").val(), 10);
        var maxVal = parseInt($("#sel_max").val(), 10);
        var bothHaveValues = !isNaN(minVal) && !isNaN(maxVal);
        if (bothHaveValues) {
            if (minVal > maxVal) {
                return options.allrules.cmm.alertText2;
            }
            else if (maxVal < minVal) {
                return options.allrules.cmm.alertText;
            }
        }       
};

jquery.validationEngine-en.js :

"cmm": {
 "alertText": "* Max value < Min value!",
 "alertText2": "* Min value > Max value!"
  },
2
  • You answer your own question after Someone gave you the instruction how to solve your problem? An answer should give you an idea or pointing out an error, it don't has to be a complete copy of the original Code. It is an explanation not the delivery of production code. You do not even write a single line which describes your posted answer. You didn't follow the correct advice of Ohgodwhy and rethink your poor Code. Dec 21, 2014 at 8:13
  • Sorry. i am novice programmer and quite new for Stackoverflow. i couldn't follow correct advice of Seniors of Stackoverflow. From the next time if i post any code, it'll be a complete explanation which'll help to others, not the copy of my original code. Thanks for letting me know about my mistake. Dec 22, 2014 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.