Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In HTML form, there are four fields: first name, last name, credit card, cvv.

XML
<div class="form-group">
                      <div class="col-sm-6">
                          <div class="input text"><label for="PlanFirstname">First name on Account</label><input name="data[Plan][firstname]" class="form-control" pattern="^[a-zA-Z\s]+$" title="Only alphabets and space are allowed" required="" type="text" id="PlanFirstname"></div>
                      </div>
                      <div class="col-sm-6">
                          <div class="input text"><label for="PlanLastname">Last name on Account</label><input name="data[Plan][lastname]" class="form-control" pattern="^[a-zA-Z]+$" title="Only alphabets are allowed" required="" type="text" id="PlanLastname"></div>                     </div>
                </div>
                <div class="form-group">
                     <div class="col-sm-6">

                    <div class="input text"><label for="PlanCardnumber">Card number (no dashes or spaces)</label><input name="data[Plan][cardnumber]" class="form-control" pattern="[0-9]{16}" title="16 digits" required="" type="text" id="PlanCardnumber"></div>                 </div>
                    <div class="col-sm-6">
                    <div class="input text"><label for="PlanCvvnumber">cvv number</label><input name="data[Plan][cvvnumber]" class="form-control" pattern="[0-9]{3,4}" title="3 to 4 digits" required="" type="text" id="PlanCvvnumber"></div>                  </div>
                </div>




For each field, required validation has been applied using required attribute and regular expression has been applied to each field using "pattern" attribute.
I want that user should not able to enter any digit after 16 digits in credit card number textbox. Validation has been applied for maximum 16 digits, but user is able to enter more than 16 digits and validation message is shown to user on submit button click. How to restrict user to enter more than 16 digits?
Posted
Comments
Maddy selva 19-Feb-15 7:56am    
Did u apply maxlength property?

<input name="data[Plan][cardnumber]" class="form-control" pattern="[0-9]{16}" title="16 digits" required="" type="text" id="PlanCardnumber" maxlength="16">
Ishpreet Kaur 19-Feb-15 11:19am    
Thanks

1 solution

you can use maxlength property of textbox

C#
maxlength="5"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900