Javascript reg ex requires an integer from 1 to 999999?

I wish I could solve this on my own, but I never understood regular expressions. They seem so powerful. I wanted to ask where is the best resource for learning javascript reg ex, but this was subjective, and I did not want my question to close. I have a web form text field, when it matters, this value must be an integer from 1 to 999999. I already use the jquery numeric widget to include only numbers, all other keystrokes are rejected, there is an onBlur implementation, what if any regular expression is not matched, a callback will be called ... This is that code ...

$.fn.numeric.blur = function()
{
var decimal = $.data(this, "numeric.decimal");
var callback = $.data(this, "numeric.callback");
var val = $(this).val();
if(val != "")
{
    var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
    if(!re.exec(val))
    {
        callback.apply(this);
    }
}
}

, , val 1 999999? . , , javascript regex? . !

Cheers,
~ ck -

+3
4

. Javascript:

if( typeof decimal === 'number' && (decimal > 1 && decimal < 999999) ) {
     // here we go
}

, + .parseInt()

var val = +$.trim($(this).val());
if( val && (decimal >= 1 && decimal <= 999999) ) {
}
+7

:

[0-9]{1,6}

/.

+5

This is not a regex problem. Take a look at the jQuery function . The validate . You can specify range requirements.

+2
source
var re = new RegExp("[1-9][0-9]{0,5}");

Should do it, and the best resource for regular expressions is probably http://www.regular-expressions.info/

+2
source

Source: https://habr.com/ru/post/1781487/


All Articles