0

I want to take input in just positive integer at front end. i also make pattern but it takes input also negative integers but i don't want negative integer in input. kindly help me.

echo "<input type=number pattern=[0-9] value='" . $counter_row['counter_balance'] . "' name=new_cash class=input_panel>";
  • possible duplicate of [jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points)](http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all) – vinayakj Jun 14 '15 at 08:16
  • @vinayakj he is not looking for decimals as per my understanding. – Sunil B N Jun 14 '15 at 08:19
  • @Zohaib Use min="0" instead of pattern property. – Sunil B N Jun 14 '15 at 08:20
  • @SunilBN the post has multiple answers posted and which he can use with little/no modification needed. – vinayakj Jun 14 '15 at 08:25

3 Answers3

1

Use HTML5 form Validation

<input type="number" min="0">
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
1

That markup looks pretty invalid. You need double quotes around the properties values and that regular expression pattern is only half finished, unclosed. This is probably what you are looking for, more or less:

echo '<input type="number" 
             pattern="[0-9]+"
             value="' . $counter_row['counter_balance'] . '" 
             name="new_cash" 
             class="input_panel">'."\n";
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • when we say type ="number" it takes negative also.. http://jsbin.com/vufafemixi/1/edit?html,js,output – Sunil B N Jun 14 '15 at 08:18
  • on negative integer entering it notify with " Please match the requested format". i want to also change this notification text to "please enter positive integer" how i can do? – Zohaib Siddique Jun 14 '15 at 08:23
  • From docs (re `pattern` attribute) "This attribute applies when the value of the type attribute is text, search, tel, url or email; otherwise it is ignored." -- `type="number"` already says enough about the type of value you want. – Mulan Jun 14 '15 at 08:25
0

Exmaple: http://jsfiddle.net/kevalbhatt18/xya4Lot0/

Use : pattern="\d+"

Now in this example if you use type="number" then at submit time only message very means it will allow only numbe and as you cansee in pattern it will allow only + number

<form>
    <input type="text" pattern="\d+" />
    <input type="submit" />
</form>


Edit:

Example: http://jsfiddle.net/kevalbhatt18/xya4Lot0/1/

<form>
    <input type="number" pattern="\d+" />
    <input type="submit" />
</form>

Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40
  • thanks for reply but its just working with only input type TEXT but i need for input type NUMBER. – Zohaib Siddique Jun 14 '15 at 08:18
  • have you tried with number http://jsfiddle.net/kevalbhatt18/xya4Lot0/1/ see this link I think it is working! – Keval Bhatt Jun 14 '15 at 08:19
  • not giving error message on entering negative integer. thanks for time giving. – Zohaib Siddique Jun 14 '15 at 08:25
  • this is just an example for pattern . the above fiddle is for testng pattern is working or not , if i use onkeypress listener then it will work on keypress but i think you are using some different type of code so i jut given pattern to test . AND you Haven't mention on key press. – Keval Bhatt Jun 14 '15 at 08:30