Help with Javascript in a php page

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
BinaryBird
Forum Newbie
Posts: 9
Joined: Tue Dec 22, 2009 1:26 pm

Help with Javascript in a php page

Post by BinaryBird »

Hi, i am trying to validate a form field using javascript in a php page. But its not working. Here is the code :

Code: Select all

<?php
     session_start();
     $pvt_id = session_id();
     //if(isset($_POST['submit'])) {
     $_SESSION['name'] = 'Jester';
     $_SESSION['id'] = $pvt_id ;
     //echo $id;
     //}
     
?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Validation</title>
  <script language="JavaScript" src="gen_validatorv31.js" type="text/javascript"></script>

</head>

<body>
      <form id="createaccount" name="createaccount" action="register.php" method="post" >
      ID:<input type="text" name="id"  id="id" size="30" value="" />
      <input style="width:19em" id="submit"  name="submit" type="submit" value='Validate' />
      </form>
      <script language="JavaScript" type="text/javascript">
      var frmvalidator = new Validator("createaccount");
       frmvalidator.addValidation("id","req","Please enter your ID")
       frmvalidator.addValidation("id","maxlen=10","Max length for ID is 10");
      </script>
</body>
</html>
Am i missing something here? Kindly help me. Thanks.
rahulzatakia
Forum Commoner
Posts: 59
Joined: Fri Feb 05, 2010 12:01 am
Location: Ahmedabad

Re: Help with Javascript in a php page

Post by rahulzatakia »

Hi,

You can use the following code..

Code: Select all


<script language="JavaScript" type="text/javascript">
     function validateform()
	 { 	
	 	var flag = 0;
	   	var x = document.createaccount.id.value;
		if(x == "")
		{
	   		alert("Please enter you ID");
			flag = 1;
		}
		else if(x.length > 10)
		{
			alert("Max length for ID is 10");
			flag = 1;
		}
	    
		if(flag == 1)
		    return false;
		else
			return true;
	   }
      </script>
<?php
     session_start();
     $pvt_id = session_id();
     //if(isset($_POST['submit'])) {
     $_SESSION['name'] = 'Jester';
     $_SESSION['id'] = $pvt_id ;
     //echo $id;
     //}
     
?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Validation</title>
  <script language="JavaScript" src="gen_validatorv31.js" type="text/javascript"></script>

</head>

<body>
      <form id="createaccount" name="createaccount" action="register.php" method="post" onSubmit="return validateform();">
      ID:<input type="text" name="id"  id="id" size="30" value="" maxlength="10" />
      <input style="width:19em" id="submit"  name="submit" type="submit" value='Validate' />
      </form>
      
</body>
</html>
 
For the max. length you can also use in html which i have used for you and i have also included it in javascript validation. You can use any of them and if you have any queries then let me know..
Post Reply