2 Replies - 803 Views - Last Post: 14 March 2013 - 07:53 AM

#1 jordfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-November 12

Javascript Validation Problem

Posted 13 March 2013 - 10:44 PM

I am trying to compare two values so that the new value entered in a form field cannot be lower than the current value stored however when i try to do this it wont retrieve the new value entered?

this is the code for the form used (the action will be set properly at a later date)
<form name="form" id="form" class="form" action="#" method="post" onsubmit="return validate();">
Current Bid <input type="text" name="bidvalue" id="bidvalue" value="<?php echo $row['p_start_price'];?>" size="5"/>
<input type="submit" value="Place Bid"/>
</form>




This is the javascript used to validate however, as it is it doesnt show anything but when i try to just display the currentbid in the alert it is always the same value and doesnt take into account what the user has entered to change it.
<script type="text/javascript">
var startprice = "<?php echo $startprice; ?>";
var currentbid = document.getElementById('bidvalue').value;
function validate()
{
	if (currentbid < startprice)
	{
		alert(currentbid);
	}
}
</script>



Any help is appreciated, Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript Validation Problem

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,227
  • Joined: 08-June 10

Re: Javascript Validation Problem

Posted 13 March 2013 - 11:25 PM

the problem is that currentbid is retrieved at the beginning (e.g. page load) where the text field is still empty. when you call the validate() function, the value has not been updated (in Java​Script), so the startprice is compared to an empty value.

what you should do is get the value for currentbid inside the validate() function.
Was This Post Helpful? 1
  • +
  • -

#3 jordfc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-November 12

Re: Javascript Validation Problem

Posted 14 March 2013 - 07:53 AM

Ah yeah i see, thank you very much for your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1