Koobi Posted July 18, 2006 Share Posted July 18, 2006 Hi,I'm not too good with JavaScript, I'm trying to figure out how I can detect if a user input in a text field has more than two decimal places.For example:1. If user inputs 10.555, I would trigger an error2. If user inputs 4.34, this should be fine3. If user inputs 1.2 this would be fine too4. If the user inputs 7, this is fineAny help?Thanks for your time Quote Link to comment Share on other sites More sharing options...
nogray Posted July 18, 2006 Share Posted July 18, 2006 You can use the indexOf() method[code]<script language="javascript"> function check_field(){ val = document.getElementById('tst').value; if (val.indexOf(".") < val.length - 3){ alert("error"); } else { alert("good"); } }</script><input type="text" id="tst" name="tst" />[/code] Quote Link to comment Share on other sites More sharing options...
Koobi Posted July 20, 2006 Author Share Posted July 20, 2006 Great, thanks for that :)Seems to work fine. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.