• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 947
  • Last Modified:

jquery single declaration; whole usage on site

I've got script (JQUERY .js) and it only works on the 1st declaration (in input); then it doesn't works on the second input. What to do?
JQUERY .js
$(document).ready(function(){$("#quantity").keypress(function(e){if(e.which!=8&&e.which!=0&&(e.which<48||e.which>57))return false})});
 
 
HTML
<input type=text name=price0 id=quantity class=align_right size=10 value='<% $_POST[price0] %>' maxlength=10 />
						  		.<input type=text name=price0_2 id=quantity size=1 value='<% if $_POST[price0] <> '' AND $_POST[price0_2] == '' %>00<% else %>00<% $_POST[price0_2] %><% /if %>' maxlength=2 /><% $currency %>&nbsp;

Open in new window

0
AndyPSV
Asked:
AndyPSV
  • 3
  • 3
1 Solution
 
brandonvmooreCommented:
I'm just learning jquery so I don't know how much help I'll be, but at first glance I'm a little confused by your keypress event.  It doesn't 'do' anything, it just returns a value of false under the right considitions.  Does returning a false value cancel the event or something?
0
 
AndyPSVAuthor Commented:
It just does what it suposse to do - allows only digits to be typed in the box (in the first input)
0
 
brandonvmooreCommented:
if (   e.which!=8 &&   //not backspace
       e.which!=0 &&   //not null
      (e.which < 48 || e.which > 57))  //less than '0' OR greater than '9'
      return false

By this logic you are exluding digits.  It should either say:

(e.which >= 48 || e.which <= 57)

or you could write it like:

!(e.which < 48 || e.which > 57)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
AndyPSVAuthor Commented:
it ain't work
0
 
brandonvmooreCommented:
Sorry, I didn't mean to infer that would fix the problem that you were asking about.  I just meant that if your goal is to only allow numbers to be typed in then even if you fixed that problem it still wouldn't work until you corrected the part that I was pointing out.

Have you tried doing this without using jquery?  You might see if you can implement it w/o using jquery just as a test to see whether the issue is related to jquery or not.
0
 
AndyPSVAuthor Commented:

$(document).ready(function(){$('#phone_cc').keypress(function(e){if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}});});

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now