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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with AND OR

Status
Not open for further replies.

PSFMIS

MIS
Feb 3, 2006
28
US
I'm trying to only allow number keys, period, decimal point, and dash.

Why does this generate an error? Something is wrong with my if statment syntax.

Code:
<SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         
		 if ((charCode >= 48 && charCode <= 57) || charCode = 189 || charCode = 190 || charCode = 110)
			 return true;
		 return false;

      }
      //-->
   </SCRIPT>

Thanks,
Aaron
 
You are using = which is an assignment operator rather than using == which is a comparator. Try this:

Code:
<SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         
         if ((charCode >= 48 && charCode <= 57) || charCode =[COLOR=red]=[/color] 189 || charCode =[COLOR=red]=[/color] 190 || charCode =[COLOR=red]=[/color] 110)
             return true;
         return false;

      }
      //-->
   </SCRIPT>

Stamp out, eliminate and abolish redundancy!
 
I don't get the syntax error anymore however it does not let me enter the dash, period, or decimal point either...

Aaron
 
BTW, I noticed your charCodes seem to be off.
48-57 are numbers 0-9 but 110 is a lower case n and 189 and 190 are 1/2 and 3/4 fractional symbols.

You said you want to check for period and decimal point but those are the same symbol.

Your best bet is to use a regular expression. This one should do what you need.

Code:
function isNumberKey(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode       
  var re = /^[\d.\-]+$/;
  return re.test(value);
}

Stamp out, eliminate and abolish redundancy!
 
I got it working like this.... Now I need to figure out how to only allow a dash if its the first character and only allow one decimal point to be entered.

Code:
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         
		 if ((charCode >= 48 && charCode <= 57) || charCode == 45 || charCode == 46)
			 return true;
		 return false;

      }
 
Actually, what I think you want to do is a test for signed integers.
Google regular expression signed integer and see what you come up with. I was looking at some functions for exactly that late last week but have not done anything with them yet.

I am only just beginning to learn regular expressions but they are very flexible and can do everything you want to do in a single line without having to have a whole bunch of if statements.


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top