Skip to content Skip to sidebar Skip to footer

Validate Character Amount, Text Length And Decimal Places From Input Using Javascript

I am using a javascript to validate input from a textbox that's inside a ASPxGridView control(DevExpress component). I am using this javascript code to validate it(thru OnKeyPress

Solution 1:

/^\d{0,3}(,\d{0,2})?$/.test(textbox.value + char);

This will match any number with as many as three pre-decimal places. Optionally, it allows a decimal and up to 2 decimal places. Also matches the empty string, for ease of use. So this will check to make sure the resultant box matches.

An explanation of the regEx:

^

Start of string

\d{0,3}

0 to 3 digits (inclusive)

(...)? 

An optional group

,\d{0,2}

A comma followed by 0 to 2 digits (inclusive)

$

End of string.

Solution 2:

var regex_test = /^[1-9][0-9]{0,2},[0-9][0-9]{0,1}$/;
varstring = '766,99';

if(regex_test.test(string)){
    console.log('good');
}

Post a Comment for "Validate Character Amount, Text Length And Decimal Places From Input Using Javascript"