Jump to content

Recommended Posts

Hi guys,

 

I have an input box that im validating that the data is numeric. The problemis with the keypress event, when i click the backspace button i want it to do nothing. but at the moment it keeps bringing up my alert and its not working properly any help would be great. also it behaves differently in ie vs firefox. same code below.

 

function validateNumeric(field,e)

{

    var value = field.value;  // get input value

   

    if(window.event) var key = window.event.keyCode; // ie

    else if(e) var key = e.which;                    // firefox

   

    if(isNaN(value)) {      // check if value is not numeric

        alert('Please enter numerical data only.');

        if(key == 8){ // backspace button

          key.returnValue=false;

        }

    }

 

}

 

<input type="text" size="10" maxlength="10" onkeypress="validateNumeric(this);">

 

so basically it kind of working with validating that the data is numeric but when they click the backspace button to remove the text from the input box if they did so it keeps bringing up the alert message in the javascript.

Link to comment
https://forums.phpfreaks.com/topic/104564-javascript-events-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.