I'm trying to check if there's a number in a text input using regular expression. Here's the code:
var regex = /^[0-9]+$/;
if (myInput.val().match(regex)) {
console.log("number");
} else {
console.log("bad");
}
It works well, but if I add text, then backspace all the way, I get "bad". How can I make it log "good" when there isn't anything in the text input? I don't want to allow spaces, but I want to allow an empty input.
I tried:
var regex = /\s ^[0-9]+$/;
But then whatever I insert in the input, I always get "bad".
/^[0-9]*$/and useRegExp#testinstead ofString#matchif (!val.length || val.match(regex))1234, as well as ona1a,1ab12andabd1? Not only on text which contains only numbers, but on number/letter as well?