---
title: "How to Prevent zero as first digit in an numeric only textfield in jquery?"  
description: "How to Prevent zero as first digit in an numeric only textfield in jquery?"  
author: "Anonymous User"  
published: 2014-10-31  
updated: 2014-10-31  
canonical: https://www.mindstick.com/forum/2479/how-to-prevent-zero-as-first-digit-in-an-numeric-only-textfield-in-jquery  
category: "jquery"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# How to Prevent zero as first digit in an numeric only textfield in jquery?

How do I restrict if the first digit is [zero](https://www.mindstick.com/forum/1264/add-a-zero-to-the-returned-value-if-there-is-no-zero) (0)\

```
$('.btn').keypress(function(e) {      var code = e.which;      if(($(this).val().indexOf(".") == -1 && code == 46) || (code >= 48
&& code <= 57) || (code == 8) || (code >= 48 && code
<= 57)){          return true;        }      
       return false;    })    .bind('paste',function(e) {    e.preventDefault();});   
```

## Replies

### Reply by Allen Scott

just try this:\

```
if(pos == 0 && code == 48) {     e.preventDefault();}
```


---

Original Source: https://www.mindstick.com/forum/2479/how-to-prevent-zero-as-first-digit-in-an-numeric-only-textfield-in-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
