Solved

Form Field Check

Posted on 2013-01-31
6
255 Views
Last Modified: 2013-02-04
I have a form written in PHP & one of the fields is the last name. I have some javascript that checks & throws a message that says "ATTENTION: Please enter a last name" if the user forgets to enter a last name/leave that field blank. I'm trying to alter the code with PHP to strip any trailing spaces or any - or ' in the last name but have had no luck. Is there a way I can use javascript to instead of stripping it from the field, to just throw a message that says you must remove the - or ' from this field before continuing sort of like this code does in case they forget to enter anythign in the field. This code will throw a message if the last name field is left blank:

if(document.forms[0].lname.value == "") {   
alert("ATTENTION: Please enter a last name.");   
document.forms[0].lname.focus();  
return(false)

Open in new window


Thanks for any help provides!
0
Comment
Question by:wantabe2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38839953
Add this to a script tag in the head. I assume 1 form on the page:
window.onload=function(){
  document.forms[0].onsubmit=function() {
    if (this.lname.value == "") {   
      alert("ATTENTION: Please enter a last name.");   
      this.lname.focus();  
      return false;
   }
}

Open in new window

0
 
LVL 15

Author Comment

by:wantabe2
ID: 38840033
Maybe this will help. Here all the code on the page. I just need to thorw some type of message if a user types in a ' or - in the lname field on line 139 & click submit...

<html
<head>
<title>New</title>
</head>
</html>

<p> <b><i>To go to the main page <a href="http://tp-o-flow/flow/index.html">click here</a>.</b></i></p>
<b> <i> This is the initial entry page. This information <u> will not </u> notify. </b> </i>

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

if( isset($_POST) && !empty($_POST) )
{
     $host	= "localhost";
     $user	= "uname";
     $pw	= "password";
     $db	= "flow";
        
     $conn = mysql_connect( $host, $user, $pw )
     or die( "Error! Unable to connect to database server: <br/>" . mysql_error() );

     $rs = mysql_select_db( $db, $conn )
     or die( "Error! Unable to connect to database:  <br/>" . mysql_error() );
          
	foreach($_POST as $key=>$value)
	{
		${$key}=mysql_real_escape_string($value);
	}
	
     $strSQL = "INSERT INTO psrinfo
     	( fname, mname, lname, location, employee, status, oth_date, dock)
     	VALUES
     	( '" . $fname . "', '" . $mname . "', '" . $lname . "',  '" . $location . "', '" .$employee ."', '" .$status ."', '".$oth_date ."', '".$dock."')";
	 	 	 
     if (!mysql_query( $strSQL, $conn )){
     	echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
     }
     else{
     	header( "Location: index.html" );
		exit;
     }
}
?>
<html>

<head>
<title> Application </title>

<script type="text/javascript">
var valid;

function d2(v) { return (v<10)?("0"+v):v; }

function confirmation() {
		msg="You're about the enter the following information:";
		msg+="\n"; 
		msg+="\ndock: " + document.forms[0].dock.value;
        msg+="\nName: " + document.forms[0].fname.value + " " + document.forms[0].lname.value;
        msg+="\nStatus: " + document.forms[0].status.value;
		msg+="\nLocation: " + document.forms[0].location.value;
		msg+="\nEmployee: " + document.forms[0].employee.value;
		msg+="\nDate: " + document.forms[0].oth_date.value;
		msg+="\n\nIf this information is correct, click OK, if not click cancel to edit.";
		var answer = confirm(msg)
	if (answer){
		// send data to server
	}
	else{
		// don't send anything and return to your page
	}
	
	
	 if(document.forms[0].fname.value == "") {   
                alert("ATTENTION: Please enter a first name.");   
                document.forms[0].fname.focus();  
                return(false)
}

	 if(document.forms[0].mname.value == "") {   
                alert("ATTENTION: Please enter a middle name. If no middle name enter 'none'");   
                document.forms[0].mname.focus();  
                return(false)
}


	 if(document.forms[0].lname.value == "") {   
                alert("ATTENTION: Please enter a last name.");   
                document.forms[0].lname.focus();  
                return(false)

}


	 if(document.forms[0].location.value == "") {   
                alert("ATTENTION: Please choose a location.");   
                document.forms[0].fname.focus();  
                return(false)
}
	
	 if(document.forms[0].employee.value == "") {   
                alert("ATTENTION: Please choose PENDING or UNASSIGNED.");   
                document.forms[0].employee.focus();  
                return(false)
}	
	
	 if(document.forms[0].oth_date.value == "") {   
                alert("ATTENTION: Please enter data in the DATE field. If there is no date, enter 0000-00-00");   
                document.forms[0].oth_date.focus();  
                return(false)
}	
	 if(document.forms[0].dock.value == "") {   
                alert("ATTENTION: Please enter dock NO. If there is no dock NO, enter NO dock");   
                document.forms[0].dock.focus();  
                return(false)
}	



}

</script>

</head>
<body>
<body style="background-image:url(FadedBG.png); background-repeat:no-repeat; background-attachment:fixed; background-position:center;">
<form method="post" action="new.php">

<b>dock No: (Example 210)</b> <br />
<input type="text" name="dock" size="30" /><br />

<b>First Name:</b> <br />
<input type="text" name="fname" size="30" /><br />

<b>Middle Name:</b> <br />
<input type="text" name="mname" size="30" /><br />

<b>Last Name:</b> <br />
<input type="text" name="lname" size="30" /><br />

<b>Status:</b> <br />
<select name="status" /> <br />
<option value="Seal">Seal</option>
<option value="Try">Try</option>
<option value="Sent">Sent</option>
</select>
</br>

<b>Location: </b><br />
<select name="location" /> <br />
<option value="OfficeA">OfficeA</option>
<option value="OfficeB">OfficeB</option> 
<option value="OfficeC">OfficeC</option> 
</select>
</br>

<b>Unassigned or Pending:
<br> </b> 
<select name="employee" /><br />
<option value="Unassigned">Unassigned</option> 
<option value="Pending">Pending</option>
<option value="Unassigned">Unassigned</option>
</select> 
</br> 

<b>Original Date: (Example YYYY-MM-DD)</b> <br />
<input type="text" name="oth_date" size="30" /><br />

<br>

<input type="submit" value="Submit" onclick="return confirmation(this.form);"/>

</form>
</body>
</html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38842557
Here

I added value="" to all fields and fixed the focus of lastname when location was wrong and moved the onclick to the onsubmit and fixed the do nothing if answer was ok.
I added an IE compatiple trim

I would not personally insist that people typed something in for something they did not have - like no date and such.

Also it is not possible to NOT select a location. Perhaps you want
<option value="">Please select</option>
there too

<html
<head>
<title>New</title>
</head>
</html>

<p> <b><i>To go to the main page <a href="http://tp-o-flow/flow/index.html">click here</a>.</b></i></p>
<b> <i> This is the initial entry page. This information <u> will not </u> notify. </b> </i>

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

if( isset($_POST) && !empty($_POST) )
{
     $host	= "localhost";
     $user	= "uname";
     $pw	= "password";
     $db	= "flow";
        
     $conn = mysql_connect( $host, $user, $pw )
     or die( "Error! Unable to connect to database server: <br/>" . mysql_error() );

     $rs = mysql_select_db( $db, $conn )
     or die( "Error! Unable to connect to database:  <br/>" . mysql_error() );
          
	foreach($_POST as $key=>$value)
	{
		${$key}=mysql_real_escape_string($value);
	}
	
     $strSQL = "INSERT INTO psrinfo
     	( fname, mname, lname, location, employee, status, oth_date, dock)
     	VALUES
     	( '" . $fname . "', '" . $mname . "', '" . $lname . "',  '" . $location . "', '" .$employee ."', '" .$status ."', '".$oth_date ."', '".$dock."')";
	 	 	 
     if (!mysql_query( $strSQL, $conn )){
     	echo( "Unable to save data to database: <br/>" . mysql_error() . "<br/>" . $strSQL . "</span><br/>" );
     }
     else{
     	header( "Location: index.html" );
		exit;
     }
}
?>
<html>

<head>
<title> Application </title>

<script type="text/javascript">
var valid;

function d2(v) { return (v<10)?("0"+v):v; }

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}

function isText(str) {
  str = str.trim();
  return str.length>0 && 
    str.indexOf("'")===-1 
    && str.indexOf("-")===-1;
}

function confirmation(theForm) {
		msg="You're about the enter the following information:";
		msg+="\n"; 
		msg+="\ndock: " + theForm.dock.value;
        msg+="\nName: " + theForm.fname.value + " " + theForm.lname.value;
        msg+="\nStatus: " + theForm.status.value;
		msg+="\nLocation: " + theForm.location.value;
		msg+="\nEmployee: " + theForm.employee.value;
		msg+="\nDate: " + theForm.oth_date.value;
		msg+="\n\nIf this information is correct, click OK, if not click cancel to edit.";
		var answer = confirm(msg)

	 if (!answer) return false;

	 if(!isText(theForm.fname.value)) {   
                alert("ATTENTION: Please enter a first name.");   
                theForm.fname.focus();  
                return false;
     }

     if(!isText(theForm.mname.value)) {   
                alert("ATTENTION: Please enter a middle name. If no middle name enter 'none'");   
                theForm.mname.focus();  
                return false;
     }


     if(!isText(theForm.lname.value)) {   
                alert("ATTENTION: Please enter a last name.");   
                theForm.lname.focus();  
                return false;
     }

	 if(!istext(theForm.location.value)) {   
                alert("ATTENTION: Please choose a location.");   
                theForm.location.focus();  
                return false;
     }
	
     if(!isText(theForm.employee.value)) {   
                alert("ATTENTION: Please choose PENDING or UNASSIGNED.");   
                theForm.employee.focus();  
                return false;
     }
	
     if(!isText(theForm.oth_date.value)) {   
                alert("ATTENTION: Please enter data in the DATE field. If there is no date, enter 0000-00-00");   
                theForm.oth_date.focus();  
                return false;
     }

     if(!isText(theForm.dock.value)) {   
                alert("ATTENTION: Please enter dock NO. If there is no dock NO, enter NO dock");   
                theForm.dock.focus();  
                return false;
     }
     return true; // submit
}

</script>

</head>
<body>
<body style="background-image:url(FadedBG.png); background-repeat:no-repeat; background-attachment:fixed; background-position:center;">
<form method="post" action="new.php" onsubmit="return confirmation(this)">

<b>dock No: (Example 210)</b> <br />
<input type="text" value="" name="dock" size="30" /><br />

<b>First Name:</b> <br />
<input type="text" value="" name="fname" size="30" /><br />

<b>Middle Name:</b> <br />
<input type="text" value="" name="mname" size="30" /><br />

<b>Last Name:</b> <br />
<input type="text" value="" name="lname" size="30" /><br />

<b>Status:</b> <br />
<select name="status" /> <br />
<option value="Seal">Seal</option>
<option value="Try">Try</option>
<option value="Sent">Sent</option>
</select>
</br>

<b>Location: </b><br />
<select name="location" /> <br />
<option value="OfficeA">OfficeA</option>
<option value="OfficeB">OfficeB</option> 
<option value="OfficeC">OfficeC</option> 
</select>
</br>

<b>Unassigned or Pending:
<br> </b> 
<select name="employee" /><br />
<option value="Unassigned">Unassigned</option> 
<option value="Pending">Pending</option>
<option value="Unassigned">Unassigned</option>
</select> 
</br> 

<b>Original Date: (Example YYYY-MM-DD)</b> <br />
<input type="text" value="" name="oth_date" size="30" /><br />

<br>

<input type="submit" value="Submit"/>

</form>
</body>
</html>

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 15

Author Comment

by:wantabe2
ID: 38845245
If I wanted this code to check for commas would this section of code look like this?

function isText(str) {
  str = str.trim();
  return str.length>0 && 
    str.indexOf("'")===-1 
    && str.indexOf("-",",")===-1;

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 38846127
no

function isText(str) {
  str = str.trim();
  return str.length>0 && 
    str.indexOf("'")===-1 &&
    str.indexOf(",'")===-1 &&
    str.indexOf("-")===-1;

Just continue - add a && and make sure only the last has an ending ;

If you have many more, we need to think about a regular expression like

[^'_-\/]
0
 
LVL 15

Author Closing Comment

by:wantabe2
ID: 38851242
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question