Click here to Skip to main content
14,928,959 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My code can't work using javascript.

When I submit my form I want to perform validation using java script....form is not validated...i submitted with null value.....So help me out....

My Registration.jsp Page Code...


ASP.NET
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">

     
function validate_form ( )
{
 
	if ( document.register.sur_name.value == "" )
		{
			alert ( "Please fill in the 'Your Sur Name' box." );
			return false;
		}
	if ( document.register.first_name.value == "" )
		{
			alert ( "Please fill in the 'Your First Name' box." );
			return false;
		}
	if ( document.register.last_name.value == "" )
	{
			alert ( "Please fill in the 'Your Last Name' box." );
			return false;
	}
	if ( document.register.address.value == "" )
	{
			alert ( "Enter your address" );
			return false;
	}
//	if ( document.emp.num.value == "" )
//	{
//			alert ( "Enter Employee Number" );
//			return false;
//	}
	
	if ( document.register.user_name.value == "" )
	{
			alert ( "Enter your username" );
			return false;
	}
	if ( document.register.pwd.value == "" )
	{
			alert ( "Enter your password" );
			return false;
	}
	if ( document.register.cpwd.value == "" )
	{
			alert ( "Plz confirm password" );
			return false;
	}
	if ( document.register.pwd.value != document.register.cpwd.value )
	{
			alert ( "Password dont match" );
			return false;
	}
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/).test(document.register.email_id.value))  
	  {  
	    alert("You have entered an invalid email address!");  
	    return false;  
	  }
	alert("sucessfully Submitted");
	<a href="log_in.jsp" />
	return true;
}
         
</script>

</head>
<body background="images/img1.jpg">
<form name="register" >
<table width="800" align="center">
  <tr>
    <td>
    <!--Header-->
    <jsp:include page="Pages/Header.jsp" xmlns:jsp="#unknown" />
    </td>
  </tr>
  <tr align="middle">
    <td align="middle">
    <table width="100%%" align="center">
      <tr valign="middle">
        
        <td valign="middle">
        <!--Content-->
       <h2 align="center">Registration Form</h2>
    <table width="500" align="center">
    <tr>
    <td align="center" valign="top" width="41%">Sur Name:<span style="color:red">*</span></td>
 
        <td width="57%"><input type="text" value="" name="sur_name" size="24"></td>
  </tr>
  <tr>
    <td align="center" valign="top" width="41%">First Name:<span style="color:red">*</span></td>
 
        <td width="57%"><input type="text" value="" name="first_name" size="24"></td>
  </tr>
  <tr>
    <td align="center" valign="top" width="41%">Last Name:<span style="color:red">*</span></td>
 
        <td width="57%"><input type="text" value="" name="last_name" size="24"></td>
  </tr>
  <tr>
    <td align="center" valign="top" width="41%">Gender:<span style="color:red">*</span></td>
    <td> <input type="radio" name="gender" value="male" checked> Male<br>
  <input type="radio" name="gender" value="female"> Female<br></br></br></td>
  </tr>
  
  <tr>
     <td align="center" valign="top" width="41%">Address:</td>
        <td width="57%"><textarea rows="4" maxlen="200" name="address" cols="20"></textarea></td>
  </tr>
  <tr>
    <td align="center" valign="top" width="41%">City:</td>
        <td width="57%"><select name="mydropdown">
<option value="Default">Default</option>
<option value="Chennai">Chennai</option>
<option value="Bangalore">Bangalore</option>
<option value="Chennai">Pune</option>
<option value="Bangalore">Mysore</option>
<option value="Chennai">Chandigarh</option>
</select></td>
  </tr>
  <tr>
    <td align="center" valign="top" width="41%">State:</td>
        <td width="57%"><select name="mydropdown">
<option value="Default">Default</option>
<option value="Chennai">Chennai</option>
<option value="Bangalore">Bangalore</option>
<option value="Chennai">Pune</option>
<option value="Bangalore">Mysore</option>
<option value="Chennai">Chandigarh</option>
</select></td>
  </tr>
  <tr>
    <td align="center" valign="top" width="41%">Username:</td>
    <td width="57%"><input type="text" value="" name="user_name" size="24"></td>
  </tr>
  <tr>
     <td align="center" valign="top" width="41%">Password:</td>
    <td width="57%"><input type="password" value="" name="pwd" size="24"></td>
  </tr>
  <tr>
     <td align="center" valign="top" width="41%">Re-type Password:</td>
    <td width="57%"><input type="password" value="" name="cpwd" size="24"></td>
  </tr>
  <tr>
        <td align="center" valign="top" width="41%">Email<span style="color:red">*</span></td>
        <td width="57%">
        <input type="text" value="" name="email_id" size="24"></td>
    </tr>
  <tr>
  	<td align="right">
      <input type="submit" value="  Submit" name="submit" >
  	</td>
    <td>
       <button type="reset">Reset</button>
    </td>
  </tr>
</table>

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


What I have tried:

I tried possible ways which i know....as describe in above code....still facing same problem....
Posted
Updated 6-May-16 2:21am
v2
Comments
ZurdoDev 6-May-16 7:19am
   
You never call validate_form().

1 solution

onsubmit Event[^]

The target function can return true/false, and thus decide whether to submit your form to action or not.


   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900