Thiyagu
Thiyagu

Reputation: 786

How To Validate Text Field not accept 0 as First digit and Whole digit using Javascript

How To Validate Text Field not accept 0 as First digit and Whole digit using Javascript. for example, Its should not accept

0,00,000,01,02,001 and so on

It will accept

10,100,10000 and so on

Any help is appreciated

Upvotes: 0

Views: 724

Answers (1)

Bhaskar Jain
Bhaskar Jain

Reputation: 1691

Get the first character of the entered string/value, and check with 0, on which event you want to check, modify accordingly.

<input id="myId" value="0123" />
$('#myId').blur(function(e){ 
var first_char = $("#myId").val().substring(0, 1);
  if (first_char == 0){
   return false;
   }
});

Hope this helps

$( document ).ready(function() {
    $('#myId').blur(function(e){ 
var first_char = $("#myId").val().substring(0, 1);
  if (first_char == 0){
   alert('first character is 0');
   }else{
   alert('first character is not 0');
   }
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="myId" value="0123" />

Upvotes: 2

Related Questions