Jump to content

Validation Form


Hazukiy
Go to solution Solved by Hazukiy,

Recommended Posts

Hi, I'm trying to make some kind of validation for my form but I've tried everything from Javascript to PHP and I can't seem to get it to work? I'll put my javascript code below but I would much rather use PHP as the validator as I don't like using alert boxes. The problem is that it's validating at all and any method that I try it just skips it and carrys on with the registration. For example I put "dd" as my username, email and password and it accepted it. Help would be much appreciated, thanks.

 

The Javascript:

<script type="text/javascript">
	function validateForm(formElement)
	{
		if(formElement.username.length < 5)
		{
			alert('Username must be more than 5 characters.');
			return false;
		}
		
		if(formElement.email.length < 5) 
		{
			alert('Email must be more than 5 characters.');
			return false;	
		}
		
		if(formElement.password.length < 
		{
			alert('Password should be more than 8 characters.');
			return false;
		}
	}
   </script>

The Form:

<div id="register-section-wrapper">
    <div id="inner-register-container">
	 <form action="" method="post" name="register_form" id="register_form" onsubmit="return validateForm(this);">
	 <table border="0" id="table-register">
	  <th colspan="2">
	   <h1>Register</h1>
	  </th>
	  <tr>
	   <td>
	    <p>Username:</p>
       </td>
	   <td>
	    <input type="text" name="username" id="username" class="frm-style" required />
	   </td>
	  </tr>
	  <tr>
	   <td>
	    <p>Email:</p>
	   </td>
	   <td>
	    <input type="text" name="email" id="email" class="frm-style" required />
	   </td>
	  </tr>
	  <tr>
	   <td>
	    <p>Password:</p>
	   </td>
	   <td>
	    <input type="password" name="password"id="password" class="frm-style" required />
	   </td>
	  </tr>
	  <tr>
	   <td>
	    <p>Retype Password:</p>
	   </td>
	   <td>
	    <input type="password" name="cpassword" class="frm-style" required />
	   </td>
	  </tr>
	  <tr>
	   <th colspan="2">
	   <input class="frm-submit-register" name="register" type="submit" value="Submit" onclick="return validateForm(this.form);" />
	   <th>
	  </tr>
	 </table>
	 </form>

The OLD PHP Validation:

<?php 
		$error_msg = "";
	
		if(isset($_POST['register'])) {
			if(empty($_POST['username'])) {
				$error_msg = "Please enter your username.";
			}
			else {
			return true;
			
			}
			if($_POST['username'] < 5) {
				$error_msg = "Username must be more than 5 characters.";
			}
			else {
				return true;
			}
		}
	?>
Link to comment
Share on other sites

I don't know much about javascript, but what I can tell you about your PHP script is that your logic is wrong.

if($_POST['username'] < 5)

This if statement would work if the $_POST was an integer. Because it is not the if statement automatically fails. You could use the PHP function strlen() which is string length and do something like this....

$string = strlen($_POST['username']);

if($string < 5){
    //Do whatever
}

Also, I would look into using the PHP function switch instead of so many if/else statements. If you have any other questions feel free to ask.

Link to comment
Share on other sites

First - are you debugging your script in your browser to see if it's throwing errors at you and you aren't catching them?? (In IE, hit F12, click on the Script tab and hit refresh on your actual webpage window.)

 

Second - your English is confusing me. Is your submit button triggering the js function? In other words, are you getting alert boxes at all?

Third - You seem to be calling your js function twice - unnecessary.

Fourth - your html is over doing it. Why put a <p> inside a <td>?

 

As for your php - you are doing:

return true;

 

after every good validation test. So - where are you returning to? If this code is in a function, they you should start with a var set to true:

$valid_input = true;

 

(note - put $valid_input in a global statement at the top of the function.)

 

and in each validation (if) test, if the input is in any wrong, set $valid_input to false, and add some text to an error message variable:

$errmsg .= "This field is bad<br>";

 

Note the .= to collect all the error messages.

 

At the end of this function do this:

 

return $valid_input;

 

and check the returned value in your calling statement and display $errmsg when you go output.

Link to comment
Share on other sites

Additionally, this says, if username is NOT empty, then return true, so it never makes it to the next if evaluation:

            if(empty($_POST['username'])) {
                $error_msg = "Please enter your username.";
            }
            else {
            return true;
            }
Link to comment
Share on other sites

First - are you debugging your script in your browser to see if it's throwing errors at you and you aren't catching them?? (In IE, hit F12, click on the Script tab and hit refresh on your actual webpage window.)

 

Second - your English is confusing me. Is your submit button triggering the js function? In other words, are you getting alert boxes at all?

Third - You seem to be calling your js function twice - unnecessary.

Fourth - your html is over doing it. Why put a <p> inside a <td>?

 

As for your php - you are doing:

return true;

 

after every good validation test. So - where are you returning to? If this code is in a function, they you should start with a var set to true:

$valid_input = true;

 

(note - put $valid_input in a global statement at the top of the function.)

 

and in each validation (if) test, if the input is in any wrong, set $valid_input to false, and add some text to an error message variable:

$errmsg .= "This field is bad<br>";

 

Note the .= to collect all the error messages.

 

At the end of this function do this:

 

return $valid_input;

 

and check the returned value in your calling statement and display $errmsg when you go output.

 

Ah I can see how this would work, thanks. As far as putting <p> tags in the <td> go that's for the headings for the form inputs. Take in mind I'm not perfecting any of my code before I've completed the BASIC functionality and made sure that everything is working correctly; this is just beta testing so I apologize for the messy code.

Link to comment
Share on other sites

I don't know much about javascript, but what I can tell you about your PHP script is that your logic is wrong.

if($_POST['username'] < 5)

This if statement would work if the $_POST was an integer. Because it is not the if statement automatically fails. You could use the PHP function strlen() which is string length and do something like this....

$string = strlen($_POST['username']);

if($string < 5){
    //Do whatever
}

Also, I would look into using the PHP function switch instead of so many if/else statements. If you have any other questions feel free to ask.

 

Yeah that looks much neater, but as far as switch functions go, I haven't really looked into them and I'm not too sure how it would look?

I've given it go anywhere, here's what I came up with.

<?php
	$username = $_POST['username'];
	$email = $_POST['email'];
	$password = $_POST['password'];
   
    switch($username)
	{
	 case ($username < 5):
		echo "Username must be more than 5 characters long.";
		break;
	 case ($email < 6):
		echo "Email must be 6 or more characters long.";
		break;
	 case ($password < 6):
		echo "Password must be 6 or more characters long.";
		break;
	 default:
		echo "The Field is empty";
	}

   ?>
Edited by Hazukiy
Link to comment
Share on other sites

  • Solution

I've managed to do the validation form by calling my Function form.

<?php 
		} 
		else {
			$usr = new Users;
			$usr->storeFormValues( $_POST );
			$n6 = 6;

			if( $_POST['password'] == $_POST['cpassword'] ) {
				echo $usr->register($_POST); 
			} 
			else {
				echo "Password and Confirm password not match"; 
			}
			
			if( $_POST['username'] < $n6 ) {
				echo "Username must be more than 6 characters.";
			}
			else {
				echo $usr->register($_POST);
			}
			
			if( $_POST['password'] < $n6 ) {
				echo "Password must be more than 6 characters.";
			}
			else {
				echo $usr->register($_POST);
			}
	   }
    ?>

Thanks for all the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.