0

Can you please help me to fix my code? I have a textbox that will accept alphanumeric but I want to include a spacebar and period..

please help me.. thank you

<html>
<head>
<script type='text/javascript'>
var specialKeys = new Array();
            specialKeys.push(8); //Backspace
            specialKeys.push(9); //Tab
            specialKeys.push(46); //Delete
            specialKeys.push(36); //Home
            specialKeys.push(35); //End
            specialKeys.push(37); //Left
            specialKeys.push(39); //Right
            specialKeys.push(38); //Up
            specialKeys.push(40); //Down
            specialKeys.push(190); //Period
            specialKeys.push(32); //Space
        function IsAlphaNumeric(e) 
            {
                var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
                var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 96 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
                document.getElementById("error1").style.display = ret ? "none" : "inline";

                return ret;
            }
</script>
</head>
<body>

<div id="error1" style="width:300px;background:#FF9999;border:1px solid #FF1919;height:18px;text-align:center;font-family:Verdana;font-size:10px;line-height:18px;display:none;">&nbsp;&nbsp;ERROR : Special Characters not allowed&nbsp;&nbsp;</div><br>
                                                <input type='text' name='customerName' style='width:400px;text-transform:uppercase' onkeypress="return IsAlphaNumeric(event);"/>

</body>
</html>
1
  • I think you should try RegEx. Commented Feb 13, 2014 at 10:06

2 Answers 2

1

You can just add (keyCode == 32 || keyCode == 46) to your check for the accepted characters:

<html>
<head>
<script type='text/javascript'>
var specialKeys = new Array();
            specialKeys.push(8); //Backspace
            specialKeys.push(9); //Tab
            specialKeys.push(46); //Delete
            specialKeys.push(36); //Home
            specialKeys.push(35); //End
            specialKeys.push(37); //Left
            specialKeys.push(39); //Right
            specialKeys.push(38); //Up
            specialKeys.push(40); //Down

        function IsAlphaNumeric(e) 
            {
                var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
                var ret = ((keyCode == 32 || keyCode == 46) || (keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 96 && keyCode <= 122) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
                document.getElementById("error1").style.display = ret ? "none" : "inline";

                return ret;
            }
</script>
</head>
<body>

<div id="error1" style="width:300px;background:#FF9999;border:1px solid #FF1919;height:18px;text-align:center;font-family:Verdana;font-size:10px;line-height:18px;display:none;">&nbsp;&nbsp;ERROR : Special Characters not allowed&nbsp;&nbsp;</div><br>
                                                <input type='text' name='customerName' style='width:400px;text-transform:uppercase' onkeypress="return IsAlphaNumeric(event);"/>

</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

sorry! period is keycode 46, just try my edited code
0

Use RegEx to validate input, like this: http://jsfiddle.net/maximgladkov/exyVL/

function IsAlphaNumeric(e) {
     var result = e.keyCode > 0 || /^[0-9a-zA-Z .?\/]$/.test(String.fromCharCode(e.charCode)); 
     document.getElementById("error1").style.display = result ? "none" : "inline";

     return result;
};

8 Comments

thank you so much max.. i have another question.. how to enable backspace there?
and also special character /? thank you so much..i really appreciate your answer
To add /? characters, just modify the regex from /^[0-9a-zA-Z .]$/ to /^[0-9a-zA-Z .?\/]$/. If my answer helped you, please, mark the answer as accepted. Thank you!
backspace doesn't work.. :( i viewed your sample but it also doesn't work for backspace..
What browser are you using? Maybe there is some difference?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.