Jump to content

Detecting decimal places


Koobi

Recommended Posts

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 error
2. If user inputs 4.34, this should be fine
3. If user inputs 1.2 this would be fine too
4. If the user inputs 7, this is fine



Any help?
Thanks for your time
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.