简体   繁体   中英

Show alert on textbox on invalid input

I know that I can restrict textbox to pass only numeric with this:

<input type="number" id="box_1">

But alert is showing only after clicking submit button. How can I show it in on-key event with JavaScript?

Go with Jquery. something like this:

<input type="text" class="numbersOnly" value="" />

And:

jQuery('.numbersOnly').keyup(function () { 
    this.value = this.value.replace(/[^0-9\.]/g,'');
});

I think you should use this : it detects if the user has entered letters

<script type="text/javascript">
    $(document).ready(function () {
        var txtbox_1 = $('#box_1');

        // Setup the event
        txtbox_1.focusout(function () {
            var last = $.data(txtbox_1, "last");
            if ($(this).val().match(/[a-z]/i))
                alert("Letters");
        });
    });
</script>

<input id="box_1" name="box_1" type="number">

Hey I think You want something like this.

Check it jsfiddle

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