Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to validate the pattern in form submit

Tags:

html

At the time of given the input in edittext field it show error if anyone will put incorrent IP Address format of IP adsress should be like these 000.000.0.000 plz help me out

IN HTML5:-

<div data-role="content">
     <form id="form">
         <div data-role="fieldcontain">
                    <label for="ip">IP Address/System Name</label>
                    <input name="ip" id="ip" type="text" pattern="((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$">                       
         </div>
         <div style="text-align:center;">                   
                    <div data-role="button" data-theme="b" data-inline="true" id="button1">Update</div>
         </div> 
</div>
like image 689
Atul Dhanuka Avatar asked Jul 17 '13 04:07

Atul Dhanuka


People also ask

How do you validate data in the form?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.

How do you check if a form is validated?

Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .

What is pattern validation?

Pattern validation is achieved using regular expressions, which is a string of characters and symbols that defines a search pattern. For example, let's say you have an Input element where you want users to enter a username.


2 Answers

For HTML5 validation to work on submit, You have to put required attribute in your input fields, you want to validate using HTML5 and you have to submit the form.

You can handle the form data submitted through javascript, and incase you do want to handle the submitted data manually then you have to specify data-ajax="false" in you form tag

For your code, try this

<div data-role="content">
 <form id="form" data-ajax="false">
     <div data-role="fieldcontain">
                <label for="ip">IP Address/System Name</label>
                <input name="ip" id="ip" type="text" required pattern="((^|\.)((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]?\d))){4}$">                       
     </div>
     <div style="text-align:center;">                   
                <input type="submit" value="Submit button" />
     </div> 
 </form>
</div>

And in javascript you can do something like

$("#form").submit(function(e){
    e.preventDefault();
    //call your method

});
like image 78
Trying Tobemyself Avatar answered Oct 21 '22 23:10

Trying Tobemyself


One route you can go uses mask.js - In your case you can have it force the user to input their data in the proper format, pre-populate the '.' characters in the IP, and stop the user from entering non-numeric values.

Mask.js

And here is a fiddle - http://jsfiddle.net/QF9Lz/2/

click in the textbox, you'll see a formatting mask appear and you will only be able to enter numeric values in the format you specified.

So, once you include mask.js in the head, you can initialize the input mask like this:

$(document).ready( function() {
    $('#ip').mask('999.999.9.999');
});
like image 21
digiliooo Avatar answered Oct 21 '22 22:10

digiliooo