Hello 2 all ! I'm in agony since I have no JS knowledge and I'm trying to figure out how to validate an text field in form. That field must be in form of nn.nnn (NumberNumberDotNumberNumberNumber) All I have figure out how to validate field for numbers but I don't have knowledge how to allow that DOT to be validated also <form name="checknum" onkeyup="return checkban()"> ID:<input name="pnum" type="text" maxlength="6"> <input type="submit" value="Submit"> </form> <script language="JavaScript1.2"> function checknumber(){ var x=document.checknum.pnum.value var anum=/(^\d+$)|(^\d+\.\d+$)/ if (anum.test(x)) testresult=true else{ alert("Please input a valid number!") testresult=false } return (testresult) } </script> <script> function checkban(){ if (document.layers||document.all||document.getElementById) return checknumber() else return true } </script> Code (markup): THE PERFECT solution would be if what ever is typed as third character that would be replaced with DOT but I would be happy if someone me help to allow and that DOT character to be validated in this field ? (or maybe a little advanced solution how to allow DOT only as third character ) CHEERS !
Hello, Thank you for your replies - I was now aware that iMask exist - it looks nice and it could be that perfect solution I will give it a trial to see what will happen - I hope there won't be any conflicts with scripts .. forms... I will post my end results here. Thanks again !
This may work for you also. <form name="checknum"> ID:<input name="pnum" type="text" maxlength="6"> <input type="button" value="Submit" onmousedown="checksubmit()"> </form> <div id="result"></div> <script language="JavaScript1.2"> function checksubmit(){ var x=document.checknum.pnum.value; // start with first character at index 0 and get 2 characters // this gets the first 2 characters entered var y=x.substring(0,2); // start with the character a index 3 (one past the dot) and get all characters to the end // this gets the last 3 characters entered var z=x.substring(3); var output = document.getElementById("result"); // check that y and z only contain numbers if(!IsNumeric(y)) { output.innerHTML = "Invalid entry"; return(false); } if(!IsNumeric(z)) { output.innerHTML = "Invalid entry"; return(false); } // if we get here than the 2 values are good; put them back together separated by a dot var goodValue = y + "." + z; output.innerHTML = "<br /><br />Value entered: " + goodValue; } function IsNumeric(strString) // check for valid numeric strings { var strValidChars = "0123456789"; var strChar; var blnResult = true; if (strString.length == 0) return false; // test strString consists of valid characters listed above for (i = 0; i < strString.length && blnResult == true; i++) { strChar = strString.charAt(i); if (strValidChars.indexOf(strChar) == -1) { blnResult = false; } } return blnResult; } </script> Check that nn is numeric, replaces next character with a dot, and then checks that nnn are numeric. Thanks. -Jim