?
Solved

Function for Alpha Numeric letters

Posted on 2003-03-24
5
Medium Priority
?
1,312 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
Comment
Question by:adnan_rais
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:hexagon47
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

by:hexagon47
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

by:johnczimm
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

by:
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

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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

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

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

Join & Ask a Question