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!

How to ensure COMMAS are not entered into form field 1

Status
Not open for further replies.

andy98

Programmer
Jul 7, 2000
120
GB
Hi

I have the following validation in my form.

<SCRIPT LANGUAGE="JavaScript1.2">
<!-- Hide from other browsers

function validate(regForm)
{

if (regForm.Firstname.value =="")
{
window.alert("Please enter your Firstname");
return false;
}

}

and my form starts like this..

<form method=post action="submit.php" onsubmit="return validate(this)">

How would I ensure that the Firstname field in my form was not blank or didn't contain commas?

Any help appreciated
 
add this to your validation function:
Code:
if ((/[,]/).test(regForm.Firstname.value)) {
   alert("Firstname field cannot contain commas");
}

-kaht

banghead.gif
 
Kaht

Thanks for that - it works a treat. I was wondering whether it would be more friendly if I stripped them out on entry into the field. Is that something I could do?
 
Sure you can do it, as far as it being friendly.... I guess that's for the users to decide.
Code:
<script language=JavaScript>

function checkForComma(obj) {
   str = obj.value;
   if (str.charAt(str.length - 1) == ',') {
      obj.value = str.substr(0, str.length - 1);
   }
}

</script>
<body>
<form name=blahForm>
<input type=text onkeyup='checkForComma(this)'>
</form>
</body>
personally I'd use the above solution so the users aren't confused about why the commas are disappearing

-kaht

banghead.gif
 
Yes - I think you're right Kaht.

Thanks for your help

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top