Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do numeric validation using JQuery?

How to do numeric validation using JQuery. I have to validate my price field using JQuery. any body knows please share your knowledge with me :(

like image 582
DEVOPS Avatar asked Jun 03 '26 10:06

DEVOPS


1 Answers

If your looking for form validation, you can look into the validation plugin: http://docs.jquery.com/Plugins/Validation

However if you just want some sample code, it could be as simple as this:

$(function() {
  $('ref-to-input').blur(function() {
    if($(this).val().match(/[^\d]/)) {
      // invalid chars detected
    }
  });
});

you could also filter your input to only allow numeric characters:

$(function() {
  $('ref-to-input').keyup(function() {
    $(this).val($(this).val().replace(/[^\d]/, ''));
  });
});

Edit: don't forget to always use server side checking

like image 91
Ben Rowe Avatar answered Jun 06 '26 01:06

Ben Rowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!