简体   繁体   中英

RegExp first character cannot be dot and only numbers

I have a regular expression that allows user input only numbers and dot not a comma(,). I should improve to disable user input first character dot. 1.2 ok but .2 is not valid

$.fn.inputFilter = function(inputFilter) {
        return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
            if (inputFilter(this.value)) {
                this.oldValue = this.value;
                this.oldSelectionStart = this.selectionStart;
                this.oldSelectionEnd = this.selectionEnd;
            } else if (this.hasOwnProperty("oldValue")) {
                this.value = this.oldValue;
                this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
            }
        });
    };

widthInput.inputFilter(function (value) {
        return /^-?\d*[.]?\d*$/.test(value) // my regExp
});

How to improve this part /^-?\\d*[.]?\\d*$/.test(value); to satisfy my need?

使用否定的前瞻:

/^(?!\.)-?\d*[.]?\d*$/

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