Regex validation for integers, with leading zeros
Below script will validate the numbers using regex it will allow 0 1 it will not allow 00,0000,01, 02 etc. Regex using javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<script language="javascript"> var text = '-1'; var thisRegex = new RegExp('^(0|[1-9][0-9]*)$'); if(!thisRegex.test(text)){ alert('fail'); } else { alert('success'); } </script> |
Regex using PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $str="dfsafdsa"; if (!preg_match("/^(0|[1-9][0-9]*)$/", $str)) {echo "invalid"; //$this->form_validation->set_message('regex_check', 'Please enter proper value for no of projects'); return FALSE; } else {echo "valid"; return TRUE; } ?> |