i have a phone number validation where only numbers are allowed on keypress and first number should be 0. this is my jquery script:
jQuery("#phone-number").keypress(function(ev){
var x = document.getElementById("phone-number").value;
// Don't ignore numbers.
if ((ev.charCode >= 64 && ev.charCode <= 91) || (ev.charCode >= 96 && ev.charCode <= 123) && x.charAt(0)!="0" ) {
alert("it should start with 0 ");
return false;
}
});
x.charAt(0)!="0"
is not working as any number can be passed as 1st digit.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="phone-number"/>
$(document).on('input','#phone-number',function(){
var phone=$('#phone-number').val();
if(phone.indexOf('0')!==0){
alert('First number must be 0');
$('#phone-number').val('');
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments