简体   繁体   中英

How to give a permission to the user to enter only number between 1 to 100 using jQuery or javaScript

I need to restrict the user to enter only number between 1 - 100 suppose if he is entering 101 i should give a popup message says please enter any number between 1 - 100?

Using jquery or javascript

thansk

if ($('#element_id').val() > 100 || $('#element_id').val() < 1) { 
    alert( "Please enter a number between 1 - 100 "); 
}

I know you asked for a popup message, but I thought I would show you the other option (expanding on George Cummins' code):

$("#element_id").keyup(function() {
    if ($('#element_id').val() > 100 || $('#element_id').val() < 1) {
        $("#validationMessage_id").show();
    }
    else {
        $("#validationMessage_id").hide();
    }
});
$('.target').change(function() {
  if (Number($('.target')[0].value) < 0 || Number($('.target')[0].value) > 100) {
    alert('Number must be less than 100');
  };
});

Basically what Devneck did, except setting the field back to empty if the value is invalid.

    $('.target').change(function () {
        if (Number($(this).val()) < 0 || Number($(this).val()) > 100) {
            $(this).val('');
            alert('Number must be less than 100');
        };
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM