Jump to content


Photo

Detecting decimal places


  • Please log in to reply
2 replies to this topic

#1 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 18 July 2006 - 05:19 AM

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

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 18 July 2006 - 04:17 PM

You can use the indexOf() method
<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" />

NoGray.com


#3 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 20 July 2006 - 04:28 PM

Great, thanks for that :)

Seems to work fine.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users