-1

Possible Duplicate:
How to allow only numeric (0-9) in HTML inputbox using jQuery?

how can I limit user input as user type in a text box?

For example, I want to accept only a number and I want to ignore user input as long as it's putting non-numeric character. So,

  1. I need to preserve the existing value if user enters invalid character
  2. it should be able to handle any other case of input changes (i.e. typing, copy paste, etc).

Thanks!!! Robert

0

2 Answers 2

0

You can validate a form in javascript using the form's onsubmit event:

<form enctype="application/x-www-form-urlencoded" method="post" onsubmit="validate(this);">
    <input type="text" name="number" />
    <input type="submit" name="submit" value="send" />
</form>

Then all that remains is validation:

function validate(form) {
    if(isNaN(form.number.value)) {
        alert('not a number!');

        return false;
    }
}

Problem is, javascript is run client-side. That means people can bypass this easily so you have to check server-side either way.

If you are sure your audience have modern browsers you can also use the HTML5 element <input type="number" />. But then again, this is client-side and you would have to check server-side.

Sign up to request clarification or add additional context in comments.

Comments

0

Using onKeyPress() event on the textbox fetch the user input and handle it accordingly however you want.

To handle copy + paste scenario, you can handle via onChange() event handling.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.