Solved

# Function for Alpha Numeric letters

Posted on 2003-03-24
Medium Priority
1,315 Views
Last Modified: 2010-05-18
Can anybody plz let me know the code in JavaScript that if I need a textfield to allow only alphabetic letters than the function results an error on entering any sort of number in that field. Similarly a textfield allows only numeric values n its function results an 'alert' error on passing any alphabetic letter. Help me getting the whole code plz...
0
Question by:adnan_rais
5 Comments

LVL 3

Expert Comment

ID: 8195544
<html>
<head>
<title>Form validation</title>
<script>
function validateNbr(){
var value=arguments[0];
var test= /^\d*\$/;
if (value.match(test)) {return true} else {return false}
}
function validateAbc(){
var value=arguments[0];
var test= /^\D*\$/;
if (value.match(test)) {return true} else {return false}
}
</script>
</head>

<body>
<form>
number
<input name="testfield" type="text">
letter
<input name="testfield2" type="text">
<input type="button" value="TEST NUMBER" onClick="if (validateNbr(this.form.testfield.value)) {alert('OK');} else {alert('not a number!');}">
<input type="button" value="TEST LETTER" onClick="if (validateAbc(this.form.testfield2.value)) {alert('OK');} else {alert('not a letter!');}">
</form>
</body>
</html>
0

LVL 3

Expert Comment

ID: 8195567
in the second function I have put var test= /^\D*\$/; where \D is everything but a digit, if you want to be more specifici substitute \D with [...] where ... is the charachter you want so [ABCDEF] will match the letters ABCDEF

hope that helped
0

LVL 1

Expert Comment

ID: 8195617
Here is what I use:
<script language="javascript" type="text/javascript">
<!--
function numbersonly(){
if (event.keyCode<48||event.keyCode>57)
return false
}
function alphaonly(){
var val = false;
if ((event.keyCode>=65&&event.keyCode<=90)||(event.keyCode>=97&&event.keyCode<=122)) {
val = true;
}
return val
}
// -->
</script>

<INPUT id="alphaOnly" onkeypress="return alphaonly();" type="text">
<INPUT id="numbersOnly" onkeypress="return numbersonly();" type="text">
0

LVL 1

Accepted Solution

Antithesis earned 80 total points
ID: 8198548
This one checks when the user removes focus and refocuses if there is any illegal input:
----------
<script language="javascript">
function checkLtr(c_elm)  {
var tst = /[^a-zA-Z]/g;
if (tst.test(c_elm.value))  {
alert("Only alphabetic input allowed.");
c_elm.focus();
}
}
function checkNbr(c_elm)  {
var tst = /[^0-9]/g;
if (tst.test(c_elm.value))  {
alert("Only number input allowed.");
c_elm.focus();
}
}
</script>
<input type="text" name="letters" onblur="checkLtr(this)">
<input type="text" name="numbers" onblur="checkNbr(this)">
0

Author Comment

ID: 8202125
Thanx very much... your code helped a lot
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
###### Suggested Courses
Course of the Month5 days, 20 hours left to enroll

#### 588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.