Click here to Skip to main content
12,243,752 members (41,516 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Javascript
Javascript is working fine in IE but not in Google Chrome and Firefox.



/* -- Allow only Alpha and Numeric.Not any Special Char -- */
 
function sAlphaNumericsValues(ObjID) {
 
    //var TextBox = document.getElementById(ObjID).value;
    var keyCode = window.event.keyCode;    
 
    if ((keyCode <= 57 && keyCode >= 48) || (keyCode <= 90 && keyCode >= 65) || (keyCode <= 122 && keyCode >= 97)) {
        return true;
    }
    else {
        return false;
    }
}

Help me please, thank you in advance.
Posted 19-Feb-13 19:38pm
Edited 20-Feb-13 10:34am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You can use this OnKeyDown or OnKeyUp event to perform your action.
It works fine for me. Hope it will help you
document.onkeydown=function(e) 
{
	e=e||window.event;
	if ((e.keyCode===116)||(e.keyCode===17)||(e.keyCode===18)||(e.keyCode===91)||(e.keyCode===92)) 
	{
		e.keyCode = 0;
		alert("Please don't perform any other action in your system.");
		if(e.preventDefault)e.preventDefault();
		else e.returnValue = false;
		return false;
	}
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hello,

You didn't provide much information on how to call that function. Anyway, I'm assuming that function is hook up to the onKeyDown event of a textbox.

Here is the modified function

function sAlphaNumericsValues(e,ObjID) {
    //var TextBox = document.getElementById(ObjID).value;

    var keyCode
    if (!e) {
        var e = window.event;
    }
    if (e.keyCode) {
        keyCode = e.keyCode;
    }
    else if (e.which) {
        keyCode = e.which;
    }
 
    if ((keyCode <= 57 && keyCode >= 48) || (keyCode <= 90 && keyCode >= 65) || (keyCode <= 122 && keyCode >= 97)) {
        return true;
    }
    else {
        return false;
    }
}

This is how to call it

<input id="Text1" onkeydown="return sAlphaNumericsValues(event,this.id);" type="text" />

Reference: http://www.quirksmode.org/js/events_properties.html[^]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try this:
function sAlphaNumericsValues(ObjID) {
 
    //var TextBox = document.getElementById(ObjID).value;
    var keyCode ;
    if(evt.which) {
        keyCode = window.event.which;
    }
    else {
        keyCode = window.event.keyCode;
    }    
 
    if ((keyCode <= 57 && keyCode >= 48) || (keyCode <= 90 && keyCode >= 65) || (keyCode <= 122 && keyCode >= 97)) {
        return true;
    }
    else {
        return false;
    }
}


--Amit
  Permalink  
Comments
manikamali 20-Feb-13 4:46am
   
Amit its not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100