Number Validation

I want to be my code like where I enter the number in inputbox(only numbers) at that time time only button should be enable, once entered number clicking on button validation should happen, if value is wrong inputbox should be clear and button should be disable, but I am unable to achieve tried in below way.

<input type="password" value="" id="pinvalid" name="pinnumber" class="pinnumber"  maxLength="4" required/>
<input type="button" class="submitpin pinnum-center enter" id="enterbtn" value="ENTRAR" disabled >


var pinNum; 
$('[id^=pinvalid]').on('keypress', validatePinNumber);
$("#enterbtn").on('click', function(e){ 
    
      if(pinNum == 2694){
           alert('yes');
        }
        else{
             alert('no');
            pinNum = "";
        }
 });


function validatePinNumber(event) {
    pinNum= $('#pinvalid').val();
     $('input[id="enterbtn"]').attr('disabled' , false);
    var key = window.event ? event.keyCode : event.which;
    if (event.keyCode === 8 || event.keyCode === 46
        || event.keyCode === 37 || event.keyCode === 39) {
        return true;
        
    }
    else if ( key < 48 || key > 57 ) {
        return false;
    }
    else return true;     
};

You might want try to isNAN() function?

It may be easier to use the pattern attribute on the input to only accept numbers 4 chars long.

pattern="[0-9]{4}"

A slightly unrelated question: Is this pin number intended to to give some form of security validation for the site? Is it meant to guard anything sensitive or important?

1 Like

If so, he’d better encrypt it on the client-side before sending it to the server for verification.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.