Released: 12/27/2004
DOWNLOAD CODE FOR THIS ANSWER
Question:
I have a form where the user has to insert either a "+" sign, a "-" sign, or can leave the field blank.

The problem is I'm finding it difficult to validate the input. I've tried using "\+" for the + sign (as it's a metacharacter) but I can't seem to get it to work.

code at the moment is very simple

I have the variables from the form and then just

Code:
if (sign.value != "\+" || sign.value != "-" || sign.value != "")
{
	alert("Your value is " + sign.value + "You can only enter a +, - or leave it blank");
        return false;
}
else return true;
where sign is the name of the field. Instead of printing the value of the + or -, the alert box is printing NaN.

Sorry if this has been answered before, I had a look and searched all over but couldn't find anything about it.

Thanks in advance for the help
Answer:
This may not be what your looking for but have you thought of using a drop-down with only those three options and therefore no validation would be required?

otherwise you could use something like this:
Code:
 
<HTML>
<HEAD>
<SCRIPT>
//* Forced +-_b - allows whitespace *//
function ForcedplusMinusBlank(o)
{
if((/[^\+\- ]/i).test(o.value))
{
	o.value=o.value.replace(/([^\+\- ])/g,"");
	if (navigator.appName != "Microsoft Internet Explorer")
	{
alert('Unacceptable characters have been removed from last entry!');
return false;
	} 
}
}
</SCRIPT>
</HEAD>
<BODY>
<INPUT onchange="ForcedplusMinusBlank(this)" 
onattrmodified="ForcedplusMinusBlank(this)" 
onpropertychange="ForcedplusMinusBlank(this)" 
type="text" id="InputBoxName" 
name="InputBoxName" size="1">
</BODY>
</HTML>



Bascially, this will work for IE and Netscape it disallows anything but entries + - or blank (whitespace).
In IE you cant even enter the values in Netscape it remoes them after you have left the field and warns you. 
Top of Page

Valid HTML 4.01!

Views: 412

Valid CSS!