2 Replies - 7765 Views - Last Post: 15 December 2010 - 01:21 PM

#1 amkg17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-December 10

Letters only in textbox

Posted 15 December 2010 - 12:02 PM

I have a button, onblur = a function call. the function should than check the textbox and make sure only characters a-zA-Z typed, otherwise should get an error message. This is the code I have but it does not work if you combine letters and numbers, only if the user types in just numbers.

<html>
<title></title>
<head>
<script language=Javascript>
        

		function letters()
	{
  			with(document.form)
  			{
    		if(isNaN(Fname.value))
			{
			
			}
       		else
       		{
       		text2.value = "";
        	alert("Name: Letters only!");
 			}
 			}
	}
</script>
</head>
<body>
Enter your full name.
<form name="form"></br>
Full name:  <input type="text" id="text2" name="Fname" size="50" onblur="letters();"  />
<br>
</form>
</body>
</html>





changing the the NaN to /a-zA-Z/ only made it not accept anything! i am not sure why this is happening.

This post has been edited by amkg17: 15 December 2010 - 12:03 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Letters only in textbox

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Letters only in textbox

Posted 15 December 2010 - 12:34 PM

Try this ...
<html>
<head>
<script language="javascript">
// From: http://www.dreamincode.net/forums/topic/205533-letters-only-in-textbox/

function alphaOnly(e) {
  var code;
  if (!e) var e = window.event;
  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;
  if ((code >= 48) && (code <= 57)) { return false; }
  return true;
}
</script>
</head>
<body>
Enter your full name.
<form name="form"></br>
Full name:  <input type="text" id="text2" name="Fname" size="50" onkeypress="return alphaOnly(event);"  />
<br>
</form>
</body>
</html>


:bananaman:
Was This Post Helpful? 1
  • +
  • -

#3 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Letters only in textbox

Posted 15 December 2010 - 01:21 PM

Quote

changing the the NaN to /a-zA-Z/

I'm sure if you entered "a-zA-Z" into the textbox it would have matched. The correct regexp to put there would be /[a-zA-Z]*/ if you want to allow 0 characters, or /[a-zA-Z]+/ if you require at least one character.

The brackets are a character class, which means "one character from here". The * and + are modifiers.

JMRKER has the best solution, but this one adheres to your specification, because this is probably an assignment, right?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1