Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    onblur="function(this)" not working

    The onblur in the form does not work at all, you will see on the second onblur I even inserted return and stil no joy.

    Hi I have the following functions in my js file:

    Code:
    // Function to ensure alphabetic characters for Name and Surname
    function validateName(formObject) {        
    var entry = formObject.value;       
    var pattern = /^[A-Za-z]{3,50}$/;  // I have set the characters to between 3 and 50 as Bob is a 3 letter name and max as per assignment is 50.      
    if (pattern.test(entry) == false) {                                   
    	window.alert("Name is not valid.Please input alpha characters only!");
    	formObject.value = "";           
    	return false;        
    	} 
    return true;   
    }
    
    // Function to ensure alphanumeric and space characters for username and password
    function validateUser(formObject) {        
    var entry = formObject.value;         
    var pattern = /^[A-Za-z0-9]{6}$/;   // Must contain 6 charachters and alphanumeric    
    if (pattern.test(entry)) {                        
    	window.alert("Username or Password must be Alphanumeric and must be at least 6 characters long!");           
    	return false;        
    	}  
    return true;          
    }
    And the following code (I have included all code as I found sometimes php messes up coding and need someone to look at the big picture.
    register.php
    Code:
    <?php
     
    require "config.php";
    require "connect_db.php"
     
    ?>
    
    <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Registration Page - 48323934 - ICT 2613</title>
    <meta name="Description" content="PHP ICT2613 Exam Assignment. 48323934 - Registration Page" />
    <meta http-equiv="content-language" content="en-za" />
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    <meta name="Author" content="S.J. Fouche, Student number: 48323934" />
    <meta name="HandheldFriendly" content="True" />
    <!-- Link to the external style sheet (CSS) -->
    <link rel="stylesheet" type="text/css" href="my_css.css" />
    <!-- Link to the external javascript file -->
    <script type="text/javascript" src="my_javascript.js"></script>
    
    </head>
    
    <body onload="hideDiv();">
    <!-- Used the include feature for the header and links on all pages -->
    <?PHP 
    require "header.txt";
    
    // Checking if the form fields are set
    if (isset($_POST['title']) && isset($_POST['firstname']) && isset($_POST['surname']) &&
    	isset($_POST['username']) && isset($_POST['password1']) && isset($_POST['password2'])) {
    	// Setting form field variable apon submit
    	$title = $_POST['title'];   
    	$firstname = $_POST['firstname'];
    	$surname = $_POST['surname'];
    	$username = $_POST['username'];
    	$password1 = $_POST['password1'];
    	$password2 = $_POST['password2'];
    	$password_hash = md5($password1);
    	
    	if (!empty($title) && !empty($firstname) && !empty($surname) && !empty($username) && !empty($password1) &&!empty($password2)) {
    		// Check if passwords match
    		if ($password1 != $password2) {
    			echo ("<script type='text/javascript'>
    			window.alert('Password fields do not match, Please re-enter.');
    			</script>");
    			}
    			
    		else {
    			// Check if username already exist
    			$query = "SELECT username FROM tb_user WHERE username = '$username'";
    			$result = mysql_query($query);
    			
    			if (mysql_num_rows($result)==1) {
    				echo ("<script type='text/javascript'>
    				window.alert(\"Username $username already exists, Please choose another username\");
    				</script>");
    				}
    			else {
    			// Still busy here want to sort onblur first
    			}
    		
    		}
    		}
    	else {
    		// If any fields are empty pop up error message
    		echo ("<script type='text/javascript'>
    		window.alert('All fields are compulsory, Please complete all fields.');
    		</script>");
        }
        }
    else {
    	// Setting the default fields for the form below
    	$title = 'Mr';
    	$firstname = '';
    	$surname = '';
    	$username = '';
    	}
    
    
    // This code should never have to run but if user typed register.php in the address bar this will run
    // Check, if user is already login, if so log user out and proceed with registration
     if (loggedin()) {
    	echo ("<script type='text/javascript'>	
        window.alert('A Username is already logged in, logging out!')
    	window.location.href='logout.php'; 
        </script>");
     }
     ?>
    
    <!-- Insert form with fields required for registration -->
    <form action="register.php" method="POST">
    	<!-- Created this div to center the table -->
    	<div align="center">
    	<table border="0">
    	<tr align ="left"><td colspan="1">Title</td>
    		<!-- Created drop down options for the title as per specifications -->
    		<td colspan="1"><select name="title">
    						<!-- Took some research but eventually found a way, I wanted the selected dropdown option chosen
    						to stay displayed if the error popped up of all fields must be completed similar to the
    						php statement or the text inputs below -->
    						<option value="Mr" <?PHP if ($title == "Mr") echo 'selected="selected" '; ?>>Mr</option>
    						<option value="Mrs" <?PHP if ($title == "Mrs") echo 'selected="selected" '; ?>>Mrs</option>
    						<option value="Ms" <?PHP if ($title == "Ms") echo 'selected="selected" '; ?>>Ms</option>
    						<option value="Dr" <?PHP if ($title == "Dr") echo 'selected="selected" '; ?>>Dr</option>
    						</select></td></tr>
    						
    	<!-- Below I added a maxlength for each input field equivalant to the requirements -->
    	<tr align ="left"><td colspan="1">First Name:  </td>
    			<td colspan="1"><input name="firstname" type="text" maxlength="20" onblur="validateName()" value = "<?PHP echo $firstname; ?>" /></td></tr>
    	
    	<tr align ="left"><td colspan="1">Surname:  </td>
    			<td colspan="1"><input name="surname" type="text" maxlength="50" onblur="return validateName()" value = "<?PHP echo $surname; ?>" /></td></tr>
    	
    	<!-- Used the maxlength to restrict input to only 6 charachters -->		
    	<tr align ="left"><td colspan="1">User name (6 characters):  </td>
    			<td colspan="1"><input name="username" type="text" maxlength="6" onblur="validateUser()" value = "<?PHP echo $username; ?>" /></td></tr>
    	
    	<!-- Used the type password as it is a password being entered -->		
    	<tr align ="left"><td colspan="1">Password (6 characters):  </td>
    			<td colspan="1"><input name="password1" type="password" maxlength="6" onblur="validateUser()" value = "" /></td></tr>
    			
    	<tr align ="left"><td colspan="1">Re-type your password:  </td>
    			<td colspan="1"><input name="password2" type="password" maxlength="6" value = "" /></td></tr>
    			
    	<tr><td colspan="1"><input type="reset" value="Clear Form" /></td><td colspan="1"><input name="submit" type="submit" value="Register Now" /></td></tr>
    	
    	<tr><td colspan="2"></td></tr>
    	
    	<tr align ="center"><td colspan="2">Or Click <a href="#" onclick="showDiv()">here</a> to log in.</td></tr>
    	
    	</table></div></form>
    
    <!-- Setup login box and table properties will be hidden onload but once login link clicked it will appear-->
    <div class="picbox" id="login_form">
    <form name="form1" method="post" action="login.php">
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr align="center"><td colspan="2" style="font-size: 120%;"><strong>Member Login </strong></td></tr>
    <tr><td align="right" width="100">Username:</td><td align="center" width="235"><input name="myusername" type="text" id="myusername" size="20" maxlength="6" /></td></tr>
    <tr><td align="right">Password:</td><td align="center"><input name="mypassword" type="password" id="mypassword" size="20" maxlength="6" /></td></tr>
    <!-- I have used a reset button bellow with the onclick to hide the login and at the same time it clears the form for the next time it is shown -->
    <tr><td align="right"><input type="reset" value="Exit Login" onclick="hideDiv();" /></td><td align="center"><input type="submit" name="Submit" value="Log me in"></td></tr>
    </table>
    </form>
    </div>
    
    <!-- PHP Coding -->
    <?PHP
    // Code below will create Code link at the center and bottom of the page
    // Tested and works with all browsers except IE9, it opens in new tab not new window, It might be a setting on my PC
    echo("<p style='position:absolute; bottom:5px; left:49%;'><a href='javascript: registercode()'>Code</a></p>");
    ?> 
    
    </body>
    </html>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Any errors in the console?
    Have you tried inserting alerts to check that the functions are actually being called?

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    no errors at all it just carries on as if code not even there

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    As Logic Ali says, "Have you tried inserting alerts to check that the functions are actually being called?"


    window.alert("Name is not valid.Please input alpha characters only!");

    A proper name may contain hyphen, apostophe and space as well as letters.

    Jean-Paul O'Hara

    What about two-letter names such as Al and Jo?

    A six-character password is too insecure. Should be at least 10 characters.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks, the specifications were given by the assignment.

    I have in the interim managed to rectify the problem with PHP via preg_match

    Below my Code
    Code:
    $pattern1 = "/^[A-Za-z]{3,50}$/";  // Use this for Name and Surname checking
    	$pattern2 = "/^[A-Za-z0-9]{6}$/";  // Use this for Username and password checking
    	
    	if (!empty($title) && !empty($firstname) && !empty($surname) && !empty($username) && !empty($password1) &&!empty($password2)) {
    		// Check if passwords match
    		if ($password1 != $password2) {
    			echo ("<script type='text/javascript'>
    			window.alert('Password fields do not match, Please re-enter.');
    			</script>");
    			}
    			
    		else if (!preg_match($pattern1, $firstname) || !preg_match($pattern1, $surname)){
    			echo ("<script type='text/javascript'>
    					window.alert(\"Name and Surname can only contain Alphabtic characters, No numeric or special characters allowed and must be more than 3 charachters!\");
    					</script>");
    		}
    		
    		else if (!preg_match($pattern2, $username) || !preg_match($pattern2, $password1)){
    			echo ("<script type='text/javascript'>
    					window.alert(\"Username and Password can only contain Alphanumeric characters, No special characters allowed and must be 6 charachters long!\");
    					</script>");


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •