Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate question 1

Status
Not open for further replies.

tamnet

IS-IT--Management
Jun 2, 2005
31
GB
I have this code so that I can validate whether two input fields have values entered.

Code:
<script language="JavaScript">
<!--
function validateform ( )
{
	valid = true;

        if ( document.frmPlanOrder.txtDate.value == "" )
        {
                alert ( "Please Select a Date." );
                valid = false;
        }

        if ( document.frmPlanOrder.txtFeeds.value == "" )
        {
                alert ( "Please Enter Number of Feeds." );
                valid = false;
        }

        
        return valid;
}

//-->

</script>

however at the moment if both fields are empty then the user gets two alerts when they hit the submit button.

Could someone explain how i could make it so that if both fields are empty then on submit they only get the first alert and then if they still only fill in the one field the next time they submit they get an alert for the seconf field.




Regards

Paul
 
Use 'if-else if', if first condition failed, second condition would be checked; if first condition passed, second condition would be omitted.
Code:
<script language="JavaScript">
<!--
function validateform ()
{
  valid = true;

      if ( document.frmPlanOrder.txtDate.value == "" )
      {
              alert ( "Please Select a Date." );
              valid = false;
      }

      [b]else[/b] if ( document.frmPlanOrder.txtFeeds.value == "" )
      {
              alert ( "Please Enter Number of Feeds." );
              valid = false;
      }

        
      return valid;
}

//-->

</script>
 
13sio,

Thank You it works Great

[2thumbsup]


Regards

Paul
 
tamnet, I have always prefered to visually indicate where the errors occur on the page as well as to give them an error message. This is one method I use for doing that.

<html>
<head>
<title></title>
<script language="javascript">

function colorchange(divid, color)
{
var colchg = document.frmPlanOrder.all(divid);
colchg.style.color = (color);
}

function validateform ( )
{
//This sets the colors back to black in case they previously had been changed to red.
colorchange("dtxtFeeds", "black");
colorchange("dtxtDate", "black");

var valid = true;
var errmsg = "";

if ( document.frmPlanOrder.txtDate.value == "" )
{
errmsg = errmsg + "Please Select a Date." + "\r\n";
colorchange("dtxtDate", "red");
valid = false;
}
if ( document.frmPlanOrder.txtFeeds.value == "" )
{
errmsg = errmsg + "Please Enter Number of Feeds." + "\r\n";
colorchange("dtxtFeeds", "red");
valid = false;
}
if ( !valid )
{
alert(errmsg);
return false;
}
return true;
}
</script>
</head>

<body>
<form name="frmPlanOrder" method="post" action="mypage.htm" onsubmit="return validateform()">
<span id="dtxtDate">Date</span><input type="text" name="txtDate">
<span id="dtxtFeeds">Number of Feeds</span><input type="text" name="txtFeeds">
<input type="submit" value="Submit">
</form>
</body>
</html>


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top