Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

jQuery Each()

I have a form with 4 text input fields that I need to make sure are numeric. Each input field has a class of .laser_numeric and the form ID is #laser_finder.

Each time there is a change on th form I want to check to make sure it is either numeric or empty.  If any are not numeric (Alpha) I open an Overlay with a message box.  The message box can be closed.

The following code only works on the first field with the class .laser_numeric.  I need it to work on all four.

Help?
$(function () {
 $("#laser_finder").change(function(e) {
	 var director = $("input.laser_numeric").val();
	 
	 if(director=="" || ($.isNumeric($('input.laser_numeric').val()))){
		 return true;
	 	} else {
	 	      e.preventDefault();
	 	      $('body').append(
	 	        $('<div/>')
	 	        .addClass('overlay')
	 	        .height(
	 	          $(document).height()
	 	        )
	 	      );
	 	      $('#numeric_values').fadeIn();
	 	     
	 	      	$('#close-dialog').click(function() {
	 	      		$('div.overlay').remove();
	 	      		$('#numeric_values').fadeOut();
	 	      	});
	 		return false;
		}
	});
});

Open in new window

Avatar of Tom Beck
Tom Beck
Flag of United States of America image

You could just prevent the user from typing anything but numbers and be done with it. Have a look at this jsfiddle example. If you need a period for decimal numbers you can modify the script to include a period.

But if you insist on using your script, the solution is to use the class name of the inputs rather than the id of the form as in this jsfiddle example.
Avatar of Robert Granlund

ASKER

How do I add a decimal point to:
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;

    return true;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial