views:

130

answers:

5

I don’t want user to allow pasting of any non Alphanumeric characters on a text box. How do I restrict this in Javascript? Thanks!!

A: 

I'm not sure how you can prevent pasting, but you can filter the contents either on the submit or on the change event.

Peter Loron
If user pastes invalid characters can I set text box value = "" on change event? how would I do this?
+1  A: 

It's better to validate form - check this great jQuery's plugin - http://docs.jquery.com/Plugins/Validation/ and you can use the "number" rule : http://docs.jquery.com/Plugins/Validation/Methods/number. Really simple and easy to tune thing!

dmitko
A: 

You can use this on blue event of text box.

function remove()
{
  var otxt=document.getElementById('txt1'); 

var val=otxt.value;

 for(i=0;i<val.length;i++)
   {
     var code=val.charCodeAt(i);
     if(!(code>=65 && code<=91) && !(code >=97 && code<=121) && !(code>=48 && code<=57))
         { otxt.value=""; return ; }    

   }
}


<input type="text" id="txt1" onblur="remove();" />

It will remove all value of text box when you input non alphanumeric value.

Deepak
I think ONBlur is only for IE. How do I make this function work for multiple browsers
@user228777: using a library like prototype or jQuery, you can `observe` the `blur` event, this will work cross browser.
Josh
@user228777 `onblur` is standard. And why don't you just try it?! It's pretty ridiculous for you to come here for help and then ignore good suggestions based in incorrect assumptions.
Josh Stodola
I have experienced this before that's why I wrote my comment. Don't be so rude. These sites are to help people.
+1  A: 

Using jQuery, this is one way to do it:

HTML:

​<form name='theform' id='theform' action=''>
<textarea id='nonumbers' cols='60' rows='10'> </textarea>
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

JavaScript:

$().ready(function(){
    $("textarea#nonumbers").keyup(removeextra).blur(removeextra);
});
function removeextra() {
    var initVal = $(this).val();
    outputVal = initVal.replace(/[^0-9a-zA-Z]/g,"");       
    if (initVal != outputVal) {
        $(this).val(outputVal);
    }
};

Try it out here.

EDIT: As remarked in the comments, the original (using the .keyup() event) would have left open the possibility of pasting via the mouse context menu, so I've added a .blur() event. .change() would have been possible too, but there are reports of bugginess. Another option is using .focusout(). Time to experiment...

chryss
Sorry, Jquery is not the option for me. Boss doesn't want to user Jquery. I am doing server side validation with .net Custom validator. But I was trying to restrict it using javascript.
Well, it's not hard to rewrite without the library functions, but you'd have to do your own testing and cross-browser cleaning-up. But needing to ask, is this supposed to run somewhere that has no access to the web? Because with Google and others (I think) hosting it, it's not necessary to host it on your own machines after all.
chryss
@user228777 - jQuery is not an option, but you've *tagged* the question with `jquery` !?
Stephen P
`.keyup` ... will this cover pasting initiated with the mouse?
Weston C
@Stephen: check the edit history. @chryss has added that tag.
BalusC
There were other answers with jQuery before, so I thought it was relevant and added it - and removed the VB tag, as it has nothing to do with it. Thanks for reverting my tagging.
chryss
@Weston - try the JSFiddle -- it will paste and immediately remove.
chryss
@chryss: `<textarea>` doesn't allow short tag. The value is its body. You should close with `</textarea>`. Else you will see the remnant of the HTML source inside the textarea.
BalusC
@BalusC - thanks, fixed. I thought it looked kind of odd... :)
chryss
Now yet your fiddle demo :)
BalusC
Okokok... done. (New URL - it has revisions.)
chryss
No, this does not work. What if you paste using Edit | Paste or context menu? That doesn't fire keyup!
Josh Stodola
You need the same function invoked `onchange` as well as `onkeyup`. More here: http://stackoverflow.com/questions/1401498/remove-any-spaces-while-typing-into-a-textbox-on-a-web-page/1401538#1401538
Josh Stodola
@chryss: when I try the JSFiddle in Safari 4, if I paste using the "Edit" menu, it doesn't change, even after I take focus out of the element. I think as Josh Stodola says, you'll need the `onchange` event.
Weston C
I never added Jquery tag. I added onBlur it works with IE but I am not sure about other browsers. I also Added onChange but when is it suppose to fire up. When I added post and I do ctrl V on text box nothing happens. It takes special characters but when I click on the page then Text box clears since it has invalid characters. So I think only onBlur is working.
I have am following fucntion on onChange and onBlur it works fine. On keyPress I wrote separate function. Most of the issues are solved for IE so far. It there are any issues I will handle thsoe later. Thank you all. function checkAlphaNumeric(textbox) { var curVal = textbox.value; if (/[^A-Za-z0-9 ]/.test(curVal)) { textbox.value = ''; } }
A: 

Assuming:

<textarea id="t1"/>

You could modify the onchange event handler for the textarea to strip out anything not alphanumeric:

document.getElementById('t1').onchange = function () {
    this.value = this.value.replace(/\W/,'');
}
Weston C