1

i want to validate my mobile no field in html page by using jquery any idea

Html

<input type="text"  required="true" validType="" name="Ph" id="ph" value=""  />

what i hv to put in validType for this

e.g when we hv to validate the email we just put email in the validType

Ashwani Singh
  • 25
  • 1
  • 7

3 Answers3

2

You can use the <input type="tel" pattern="[\+]\d{2}[(]\d{2}[)]\d{4}[\-]\d{4}" />

Refilon
  • 3,334
  • 1
  • 27
  • 51
2

Use this Jquery onchange function

$("#ph").change(function() {
        var a = $("#ph").val();
        var filter = /^[7-9][0-9]{9}$/;

        if (filter.test(a)) {
            alert("valid");
        }
        else {
            alert("not valid");
        }
    });

Check this fiddle http://jsfiddle.net/GxuA7/35 click on this link and see the vaildation

Eugine Joseph
  • 1,552
  • 3
  • 18
  • 40
Choco
  • 1,054
  • 1
  • 7
  • 20
0

Here is the working code :

<body>
<input type="text" id="phone" name="txt"/>
 <input type="submit" id="submit" name="Submit" value="Submit Form"/>

 <script type="text/javascript">

$(document).ready(function(){
    $(document).on("click","#submit",function(e){
        var mobile = $("#phone").val();
        var mob_length = mobile.length;
        var first = mobile.substring(0,1);

        if(mob_length != 10)
        {
            e.preventDefault();
        }
        else if((first == 9 || first == 8 || first == 7))
        {
            alert("Working");
        }
        else
        {
            e.preventDefault();
        }

    });
});

Robin
  • 471
  • 6
  • 18