Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ensure only numbers are entered into a field. 1

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
My form has a SSN field. I need to make sure that the user only enters numbers into the field.

Right now, my script checks to make sure the field is not left empty, but does not enforce numbers only. Can anyone help me with this? I'd like it to display a different message if the user enters anything other than numbers. Like, "Your social security number can only contain numbers."

Thanks,

Here's the pertinant form fields:
Code:
<input type="text" name="ssn">
<input type="image" src="pics/btn_submit.gif" width="80" height="19" name="submitit" onclick="return dosubmit()">

Here's the code for the script:
Code:
function dosubmit()
	{
	error = "";
	{
		if(document.frm_page.ssn.value == "")
			{
			error = error + "\n" + "You must provide your social security number.";
			}
		
	if(error != "")
		{
		alert("----------- Error! ------------" + error);
		}
	else
		{
		document.frm_page.submit();
		}
	}
	}
 
Code:
if(isNaN(document.frm_page.ssn.value)) alert("Your social security number can only contain numbers")

--------------------------------------------------
Goals are dreams with deadlines
-------------------------------------
 
however, judging from your code above - may i suggest calling your validation function from the form's onsubmit event, like so:

Code:
<form ... onsubmit="return dosubmit();>

an image input type is treated like a submit button - this code, it seems, would submit regardless.

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Thanks folks. I was able to use DoubleV's suggestion and make it work with my code:

if(isNaN(document.frm_page.ssn.value))
{
error = error + "\n" + "Your social security number can only contain numbers.";
}

Appreciate the help!
 
Why not use cory's suggestion? It will return much more accurate results than DoubleV's suggestion of using isNaN(). As a matter of fact, as a general rule of thumb I don't even use isNaN() because regexp can do the exact same thing and are about 999999 times as powerful.

Using DoubleV's suggestion, what if:
[ol]
[li]Someone entered a 10 digit number[/li]
[li]Someone put in a negative number[/li]
[li]Someone put in a decimal number[/li]
[/ol]

DoubleV's suggestion would validate all 3 of these situations even though they are not valid SSN numbers. In all fairness Cory's would not validate correctly on the first item, but it would on the 2nd two (and length was not an issue in your original post).

I'm not trying to pick on you by any means DoubleV, but I'm mostly just confused by the fact that you were awarded the star for your suggestion and cory wasn't even given a thank-you even though (once again, no offense) his suggestion was better. In all honesty, I would probably write it up something like this to check for length as well as negative/decimal numbers. Copy/Paste this into a new HTML file to see why DoubleV's suggestion falls short and regexp is definitely the way to go. You'll see that using isNaN you'll be able to validate all sorts of numbers that would not pass as valid SSN numbers, but the regexp will restrict the user to entering between 1 and 9 numbers, and nothing else - perfectly valid SSN (at least I think, I know that SSN can be 8 digits, I'm unsure of how low they can actually go, this is easily fixed by changing the 1 to a different number).
Code:
<html>
<head>
<script type="text/javascript">
function validateForm() {
   var txt = document.getElementById("ssn");
/* first using your code */
   if (isNaN(txt.value)) {
      alert("Test 1 determined you have an invalid SSN");
   }
   else {
      alert("Test 1 determined your SSN is valid");
   }
/* now using a regexp */
   if (/^\d{1,9}$/.test(txt.value)) {
      alert("Text 2 determined your SSN is valid");
   }
   else {
      alert("Text 2 determined you have an invalid SSN");
   }
}
</script>
</head>
<body>
<input type="text" id="ssn" value="-534212341.212341234">
<input type="button" value="validate" onclick="validateForm()">
</body>
</html>

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
because that's a lot of people.
We seem to get that a lot on these boards Cory, hence my lack of posts lately......

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
which is a shame cause my sheep friend really needs the exposure

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
I've never seen any consistency to the "purple star" system. Experienced users know to basically ignore it, but it is damaging because inexperienced users might only home in on the "expert" postings. I too have made posts that I feel are exactly on target. Three messages later, someone regurgitates essentially the same answer and gets a "star". Oh well. This is an off-topic rant, I know, so I'll stop here.

Thomas D. Greer
 
I'm not trying to pick on you by any means DoubleV, but I'm mostly just confused by the fact that you were awarded the star for your suggestion and cory wasn't even given a thank-you even though (once again, no offense) his suggestion was better.
no offence taken. as you guys have already mentioned, the star system is far from perfect. most likely, what I posted seemed simpler then the other code, so the original poster was more inclined to use that. but of course it absolutely does not mean that my solution was better then Cory's.

--------------------------------------------------
Goals are dreams with deadlines
-------------------------------------
 
Kaht and others. Just so you know, I was looking for a quick fix...and something that fit easily into my EXISTING script. Just pop it in, and the client gets what they asked for. Plus I'm not an expert in JS so I wouldn't have known that DoubleV's post wasn't the bomb.

DoubleV does deserve my star b/c his solution solved my problem. Nothing against cLFlaVA either. cLFlaVA has helped me countless times on this board, and I did after all thank him for his help.

So take your purple stars and shove 'em folks! Ha ha ha...just kiddin.

Thanks again guys.
 
So much for the polite nudge....

Have a star Cory.

And you Mr. Sheep, have the exposure you so rightly deserve!
headbang.gif


-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
word

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top