jquery - Validating Dimension text input javascript -
how validate asp textbox javascript. want validate users input 4-1/2 or 80-1/2 or 6/8 or 5. there types of patterns there allowed , contains 1 number. want show alert when input doesn't match.
beginner javascript.
my code tried.
function valid_dimentions(fieldobj) { // regular expression var rgexp = new regexp(""/^[0-9/-]{1,4}$/""); var input = fieldobj.value; if (input.match(rgexp)) alert("the width field format incorrect!"); }
i used money , works , used build non working code.
function valid_number(fieldobj) { if (fieldobj.value != '' && isnan(fieldobj.value)) { alert('you must enter valid price 0.00 , no $'); fieldobj.select(); fieldobj.focus(); return false; } return true; }
...
<asp:textbox id="twidth" type="text" runat="server" placeholder="4-1/2" onchange="valid_dimentions_width(this);" list="parttype" runat="server" required="required" width="210px" autocompletetype="disabled" autopostback="true"/>
- avoid using
alert
. instead, fill element error message. - avoid validating on client only, validate on server side well.
- your
if
displaying error when match true. should negate it. - your regex wrong, it's checking if it's 1 4 digit number.
- you can declare regex enclosing between
/
's
here's regex came with:
^([0-9]+|[0-9]+-[0-9]+\/[0-9]+|[0-9]+\/[0-9]+)$
it checks if number 1 of following:
- pure number:
[0-9]+
- number folowed
-
followed number/number:[0-9]+-[0-9]+\/[0-9]+
- number/number:
[0-9]+\/[0-9]+
see have scape /
\
regex definition not messed up.
here's final code:
function valid_dimensions_width(fieldobj) { var error = "" var rgexp = /^([0-9]+|[0-9]+-[0-9]+\/[0-9]+|[0-9]+\/[0-9]+)$/; var input = fieldobj.value; if (!input.match(rgexp)) error = "the width field format incorrect!"; document.getelementbyid("twidtherror").innerhtml = error; }
and working fiddle: http://jsfiddle.net/w3f5skp8/
i replaced onchange
onkeyup
shows feedback instantly.
Comments
Post a Comment