We help IT Professionals succeed at work.

How to Validate Form - Compare Two Fields only if field two is selected. PHP Javascript

usiff
usiff asked
on
333 Views
Last Modified: 2010-08-05
Hello All,

form.php (listed below)

Javascript Help.  I want to validate the reserve to the start, only if the reserve is filled with data.    Note reserve is not required. - But, if reserve is filled with data if must be more than start.

I was thinking javascript, maybe when the user selects the reserve field and then jumps to the next field, I could validate.   I have been going nuts... I can't figure this one out...  

If (reserve != "" && reserve < start) {
display  "error message" next to the reserve box on the form.  
}

Thanks for all your help....

form.php

<form id="form1" name="form1" method="post" action="">
  <label>Start
Price:  
<input type="text" name="start" />
  </label>
  <p><label>Reserve Price:
    <input type="text" name="reserve" />
    </label>
  </p>
  <p><label>notes:
    <input type="text" name="notes" />
    </label>
  </p>
  <input name="" type="submit">
</form>

Comment
Watch Question

if ($_POST['reserve']!='') {
if ($_POST['start']==$_POST['reserve']) {echo('same');} else {echo('different');}
 }
If reserve is filled then it will compare reserve and start. If they are same it'll write same else different.

Hope you ment this.

Author

Commented:
I  would like this to trigger when the user selects the reserve field and then moves out to the next field on the form.   Not during the "Submit" Button.

Author

Commented:
Hello Rok-Kralj,

This works for the On Submit  Button...

But, I would like to validate on the fly...  Maybe AJAX..???  

<html>
<head>
</head>
<body>
<?php

if ($_POST['reserve']!='' && ($_POST['start'] >  $_POST['reserve'])) {
      echo('Reserve Price Not Correct... Try Again...');
      }
 ?>

<form id="form1" name="form1" method="post" action="">
  <label>Start
Price:  
<input type="text" name="start" />
  </label>
  <p><label>Reserve Price:
    <input type="text" name="reserve" />
    </label>
  </p>
  <input name="" type="submit">
</form>
</body>
</html>
No, then do it in javascript.. Because you posted in PHP section i thought that you need php solutiion. But I'm javascript expert, too.

Is that behaviour ok:

When reserve is empty: disable start field
When user types anything in reserve: start fiels not disabled

Just tell me, then I'll write you code.

Author

Commented:
Hello Rok-Kralj,

The Start Field Is Required no matter what. Only when the Reserve Field is selected and filled in with data would I need to validate and compare... if the Reserve needs to be greater than the Start.  Or Error Out.

Thank you...  

Author

Commented:
I have to run out... for a few hours...  Thanks again!
Oh, then on field's onChange run a function validate();

function validate() {
if (document.getElementById(reserve').value!='') {
if (document.getElementById(reserve').value!=document.getElementById('start').value) {
alert('Wrong input!');
}
}
}

<form id="form1" name="form1" method="post" action="">
  <label>Start
Price:  
<input type="text" name="start" id="start" />
  </label>
  <p><label>Reserve Price:
    <input type="text" name="reserve" id="reserve" />
    </label>
  </p>
  <p><label>notes:
    <input type="text" name="notes" />
    </label>
  </p>
  <input name="" type="submit">
</form>

Be careful as I set IDs to inputs in HTML.

Author

Commented:
This is what I have...  It's not working....  I want the reserve to alert only if it's less than start...
Thanks again...

<SCRIPT>
function validate() {
if (document.getElementById('reserve').value!='') {
if (document.getElementById('reserve').value < document.getElementById('start').value) {
alert('Wrong input!');}}}
</SCRIPT>

<form id="form1" name="form1" method="post" action="">
<label>StartPrice: <input type="text" name="start" id="start" /> </label>
<p><label>Reserve Price:  <input type="text" name="reserve" id="reserve" onChange="javascript:validate(this.value);"></label> </p>
<p><label>notes:  <input type="text" name="notes" />  </label> </p>
<input name="" type="submit"></form>


Author

Commented:
This code below seems to work to get the "alert box" to pop up...

I was looking to display an error message right next to text field on the form.  

Then one more question...

Everything seems to work fine the first time..  Example, I enter a 9 in start and then enter a 7 in reserve.. a pop up alert will display the message.  Here is the issue, I will change the 7 to a 6 and the alert will not pop up.?  Any clues?

Thanks....

Author

Commented:
<SCRIPT>
function validate() {
if (document.getElementById('reserve').value!='') {
      var res = document.getElementById('reserve').value;
      var sta = document.getElementById('start').value;
      if (res<=sta) {
      alert('Wrong input!');
      }
      }
      }
</SCRIPT>

<form id="form1" name="form1" method="post" action="">
<label>StartPrice: <input type="text" name="start" id="start" /> </label>
<p><label>Reserve Price:  <input type="text" name="reserve" id="reserve" onChange="javascript:validate(this.value);"></label> </p>
<p><label>notes:  <input type="text" name="notes" />  </label> </p>
<input name="" type="submit"></form>
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks... again...

Now...back to the code.....

Later....

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.