I want my form to be validated that is no field should be empty.....i have tried some code on field only but its not working...please help

<html>
<head>
<title>city talkies acccount</title>
<style type="text/css"><!--


    p {  font-size: smaller; font-family: sans-serif;align:center;}
    -->
</style>
<body bgcolor="">
<script type="text/javascript">



function validate_form ( )
{
    valid = true;

        if ( document.input.firstname.value == "" )
        {
                alert ( "Please fill in the 'Your Name' box." );
                valid = false;
        }

        return valid;
}



</script>
if(firstname==null)
{
alert("enter your name");
}
</script>
<br>
  <table width=100% align="center" bgcolor="CCCCFF"style=margin-bottom:5>
  <tr>  
 <td   style="font-family:arial;text-align:center;font-weight:bold;font-size:20;"><b>Create      your     Account</b></td>   
<image src="earth.gif"/>
</tr></table><br>
<br>
<br>
<br>    
<table  align="center" bgcolor="F0F8FF">
<tr>
<td>

<h2 align="center"></h2>
<br>
<br>

<p>
<form name="input" action="showlogin.jsp" onsubmit="return validate_form ( );">
">
<table>
<tr>
 <td align="right"
              valign="middle" width="100% font="verdana">


First  name: 
<input type="text" name="firstname" >
<br>
Last  name: 
<input type="text" name="lastname">
<br>
Address:
<input type="text" name="address">
<br>
Mobile.no.:
<input type="text" name="number">
<br>
Email-id:
<input type="text" name="email">
<br>
*Username: 
<input type="text" name="user">
<br>
*Password:
<input type="password" name="password">

<br><p align="center"><input type="submit" 
           value="create my account">
  </input>

</p>
</p>
</td>
</tr>
</table>
</form>
</td></tr></table>
</body>
</html>

Recommended Answers

All 5 Replies

1. You dont need to declare any boolean variable not unless you are validating Radio Button, check box or dropdown list, but for text boxes u dont need to do that

U can do it either this way

function validate_form ( )
{

if ( document.input.firstname.value == "" )
{
	alert ( "Please fill in the 'Your Name' box." );
	document.input.firstname.focus();
	return false;
}
else if(document.input.lastname.value == "")
{
	alert("Please enter you last name");
	document.input.lastname.focus();
                return false;
}else{
 	return true;
        }
}

and u can continue with all ur text boxes using else if statements until the last one before
the else part ok

or u can do it this way

function validate_form ( )
{
   var txtFName = document.input.firstname.value;
   var txtLname = document.input.lastname.value;

if ( txtFName == "" )
{
	alert ( "Please fill in the 'Your Name' box." );
	document.input.firstname.focus();
	return false;
}
else if(txtLname == "")
{
	alert("Please enter you last name"
	document.input.lastname.focus();
	return false;
}else{
 	return true;
        }
}

Thankyou so much Traicey, its working now.

i want my email field to be validated such that a user can enter only valid email id...how should i do this?

Put Logic!!

You need to google something called Regex or Regular Expressions.

> i want my email field to be validated such that a user can enter only valid email id...how
> should i do this?

This check can't be reliably done at client side considering that there are a lot of valid email formats. For e.g. mail provider A might not allow dots in your mail address but since mail provider B allows dots, you have to take into consideration both the options. The only reliable way of checking the validity of an email address is to introduce the concept of 'confirmation mail' in your sign up process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.