uneeb uneeb - 2 months ago 19
Javascript Question

do not allow user to enter more value if first digit is zero

i am doing some custom validation on my front end using java script what i want to achieve is if user is entering any digit other than zero it should proceed what ever he enter's after wards but if the user has entered a zero as a first digit it should not allow the user to enter any value after that!! Just the 0 nothing else

there is a functionality over many websites that i saw if they have unwanted characters in an input field they just backspace it dynamically!! like on many payment processing websites on card number field! so in my case if first digit is zero i will backspace everything that comes after that and if not zero then do nothing let it process..something like that i am looking for
here's the initial code i am working on

<?php $numbers_var='REAR_PRICE_'.str_replace(" ","_",$buttonloop[$i]);?>
<input onkeyup="checknumbers('<?php echo $numbers_var?>')" onkeypress='return event.charCode >= 48 && event.charCode <= 57' name="REAR_PRICE_<?php echo str_replace(" ","_",$buttonloop[$i]);?>"style="color: black;width: 100%;" value="<?=$rear?>" type="input"/>

and here's the javascript

function checknumbers(i){
//what to do?


You can do it all in your keypress event

function checknumbers(event) {
  if(event.target.value === '0' || !(event.charCode >= 48 && event.charCode <= 57)) {
    return false
  style="color: black;width: 100%;"