How to indicate an error if it crossed maxlength in jQuery? -


I used maxlength in my form for input fields, I also jquery.validate Indicate some error messages used js.

  • If I have used maxlength in my html alone, then small pop-ups appear only.
  • If I use legitimate alone, I'll get the appropriate error messages but the user can enter more than the limit.

However, I do not really want this scenario ...

What do I need once it has reached its maximum length , It should show an error message and this user should not allow more than 40 characters to enter.

I do not want to use jquery custom to count on the function !

I have tried with the following code:

  & lt; Form id = "myForm" & gt; & Lt; Input name = "myName" is required maxlength = "40" /> & Lt; / Form & gt;  

My jQuery code is as follows:

  $ ("# myForm"). Valid ({Rule: {myName: {expected: true, maximum length: 40},}, Message: {Maximum length: "You exceeded the maximum limit"}});  

  • Remove the HTML5 attributes from your input Elements When you have already declared the rules within .validate () , then you do not need them. I think the automatic HTML5 web browser verification message was confusing you while troubleshooting your broken jQuery validity.

  • When you specify a custom message using the Message option, you should also use name < / Code>, the rules option will specify the formatting. Only rule names are optional within message In other words, you can keep a custom message on your input name and it will automatically apply to all the rules of that input though , You can not place a custom message on the rule that is assigned according to the input you made.

Otherwise, I do not see the problem in your code ...

Demo:

HTML :

  & lt; Form id = "myForm" & gt; & Lt; Input name = "myName" /> & Lt; / Form & gt;  

jQuery :

  $ (document) .ready (function () {$ ("# myForm"). ({Rule: {myName: {expected: true, maxlanta: 40} // <- - removed before commas), Message: {myName: {// <- input name requires maxlength: " You have exceeded a maximum limit "}}});});  

Comments