0

I have to validate the textbox to enter only alpha numeric characters. The function validateAlphaNumeric(evt, txtbox) fires onkeypress event on textbox. Below is the function written in Javascript.

But I am not able to get the value of the textbox if I do Ctrl+V. I need to validate if user pastes. Can any one suggest me on this?

function validateAlphaNumeric(evt, textBox) {
    /*  File Description    :   Numbers,Characters,Hyphen(-),Slash(/)and Space */
    var charCode;   

    charCode = (evt.which) ? evt.which : window.event.keyCode;

    if (charCode >= 97 && charCode <= 122 || charCode >= 65 && charCode <= 90 || charCode == 8 || charCode >= 48 && charCode <= 57 || charCode == 45) {
        return true;
    }
    else {
        var errorMsg = document.getElementById(textBox.id + 'Error');
        if (errorMsg != null) {
            errorMsg.innerText = "Please Enter Alpha – Numeric Characters only";
        }
        return false;
    }        
}
Simon Kuang
  • 3,870
  • 4
  • 27
  • 53
Anand
  • 1
  • 2
  • You could try calling `validateAlphaNumeric()` on the `blur()` function of the input. That will recall your validate function when the user leaves the input field (tabs or clicks somewhere else) – Jake May 22 '14 at 05:20
  • here is an example http://stackoverflow.com/questions/4532473/is-there-an-event-that-occurs-after-paste – Jester May 22 '14 at 05:21

3 Answers3

0

validate text box on onblur() event of that TextBox. your problem will sure get solved.

Venki
  • 535
  • 2
  • 8
  • 21
0

from html5 on there is the oninputevent which does exactly what you want.

https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.oninput

In the handler, check that the value property of the text box contains only allowed characters.

Note that IE9 has buggy support for this event - basically it doesn't recognize character deletion, but it doesn't affect you because you can't make the text invalid just by removing stuff. See here for more detail:

http://help.dottoro.com/ljhxklln.php

If you can't use oninput because you really need to support IE8-IE7 (hint: you don't really want to) you can instead fix your code by listening to the onpaste event too to get text paste events.

pqnet
  • 6,070
  • 1
  • 30
  • 51
  • Hi I tried with the onblur..but didn't work... and I m not using HTML5.. Can any one suggest me how to make changes in the above code? – Anand May 22 '14 at 07:00
  • `onblur` will trigger when the focus leaves the text area, i.e. you get the error message when you click outside of the text area, so it is not exactly the same as what you are doing now. – pqnet May 22 '14 at 07:15
0

I have found an answer: Try this on onpaste event. Surely will work out.

I tried this:

function onPaste(evt, textBox) {
    pastedText = window.clipboardData.getData('Text');

    if (pastedText matches regExp) {
        return true;
    } else {
        //display error msg
        return false;
    }
}

Regards..

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
Lak
  • 146
  • 2
  • 15