Php validation in php for passord:
How to create retype password validation:
help me friends for retype pass validation in php:
$errorMsg = "";
if((!$email) || (!$password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$email){
$errorMsg .= "--- Email Address";
}
else if(!$password){
$errorMsg .= "--- Password";
}
}
else {
echo"send to email activation";}
This is my form for registration
<tr>
<td><div align="right">Email: </div></td>
<td><input name="email" type="text" value="<?php echo "$email"; ?>" /></td>
</tr>
<tr>
<td><div align="right"> Password: </div></td>
<td><input name="password1" type="password" value="<?php echo "$password"; ?>" />
<font size="-2" color="#006600">(letters or numbers only, no spaces no symbols)</font></td>
</tr>
<tr>
<td><div align="right"> Retype Password: </div></td>
<td><input name="password" type="password" value="<?php echo "$password"; ?>" />
</tr>
<tr>
<td><div align="right"></div></td>
<td><input type="submit" name="Submit" value="Submit Form" /></td>
</tr>
you can check if $password and $password are same
if($_POST['password '] == $_POST['password1']) echo ' Password Matches';
Try this
<?php
$errorMsg = "";
if((!$email) || (!$password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$email){
$errorMsg .= "--- Email Address";
}
else if(!$password){
$errorMsg .= "--- Password";
}
}
else {
if($_POST['password1']!=$_POST['password']){
$errorMsg .= "Passwords mismatches";
}
else{
echo"send to email activation";
}
}
?>
If you check only the equalness of two passwords, the empty == empty will true, so lets check them in one expression.
Suggestion
define min length of typed password e.g. 5
use isset because in $_POST the variable with 'password' or 'password1' key might be missing, and you'll receive notice message.
now check
...
if(isset($_POST['password']) && isset($_POST['password1']) && strlen($_POST['password']) >=5 && $_POST['password'] === $_POST['password1']) {
// password validated
}
...
also use trim() function getting email, but not password, because whitespaces can be part of password.
so $email = trim($_POST['email']); // removing whitespaces from left and right
Related
I'm trying to create a form that will create a user in my database, so they can have a profile page. I'd also like the form to send an email to confirm activation. Where is my form disconnecting? As of right now, I'm not logging any content in my db and no email is being sent.
<?php include ("session.php"); ?>
<?php // Set error message as blank upon arrival to page
$errorMsg = "";
// First we check to see if the form has been submitted
if (isset($_POST['username'])){
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Filter the posted variables
$username = ereg_replace("[^A-Za-z0-9]", "", $_POST['username']); // filter everything but numbers and letters
$address = ereg_replace("[^A-Z a-z0-9]", "", $_POST['address']); // filter everything but spaces, numbers, and letters
$city = ereg_replace("[^A-Z a-z0-9]", "", $_POST['state']); // filter everything but spaces, numbers, and letters
$state = ereg_replace("[^A-Z a-z0-9]", "", $_POST['city']); // filter everything but spaces, numbers, and letters
$accounttype = ereg_replace("[^a-z]", "", $_POST['accounttype']); // filter everything but lowercase letters
$zip = ereg_replace("[^a-z]", "", $_POST['zip']); // filter everything but lowercase letters
$name = ereg_replace("[^A-Z a-z0-9]", "", $_POST['name']); // filter everything but spaces, numbers, and letters
$fax = ereg_replace("[^A-Z a-z0-9]", "", $_POST['fax']); // filter everything but spaces, numbers, and letters
$company = ereg_replace("[^A-Z a-z0-9]", "", $_POST['company']); // filter everything but spaces, numbers, and letters
$website = ereg_replace("[^A-Z a-z0-9]", "", $_POST['website']); // filter everything but spaces, numbers, and letters
$numemployees = ereg_replace("[^A-Z a-z0-9]", "", $_POST['numemployees']); // filter everything but spaces, numbers, and letters
$yearsbusiness = ereg_replace("[^A-Z a-z0-9]", "", $_POST['yearsbusiness']); // filter everything but spaces, numbers, and letters
$annualrevenue = ereg_replace("[^A-Z a-z0-9]", "", $_POST['annualrevenue']); // filter everything but spaces, numbers, and letters
$industrysector = ereg_replace("[^A-Z a-z0-9]", "", $_POST['industrysector']); // filter everything but spaces, numbers, and letters
$preferredcontact = ereg_replace("[^A-Z a-z0-9]", "", $_POST['preferredcontact']); // filter everything but spaces, numbers, and letters
$referralsource = ereg_replace("[^A-Z a-z0-9]", "", $_POST['referralsource']); // filter everything but spaces, numbers, and letters
$email = stripslashes($_POST['email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = ereg_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
// Check to see if the user filled all fields with
// the "Required"(*) symbol next to them in the join form
// and print out to them what they have forgotten to put in
if((!$username) || (!$address) || (!$state) || (!$city) || (!$accounttype) || (!$email) || (!$password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$username){
$errorMsg .= "--- User Name";
} else if(!$name){
$errorMsg .= "Please Enter Your Full Name.";
} else if(!$phone){
$errorMsg .= "Please enter your Phone Number.";
} else if(!$fax){
$errorMsg .= "Please enter your Fax Number.";
} else if(!$email){
$errorMsg .= "Please enter your Email Address.";
} else if(!$address){
$errorMsg .= "Please enter your Address.";
} else if(!$city){
$errorMsg .= "Please enter the City in which you reside";
} else if(!$state){
$errorMsg .= "Please enter the State in which you reside.";
} else if(!$zip){
$errorMsg .= "Please enter the Zip Code in which you reside";
} else if(!$company){
$errorMsg .= "Please enter the name f your Company.";
} else if(!$website){
$errorMsg .= "Please enter your company website.";
} else if(!$numemployees){
$errorMsg .= "Please enter the current number of employees at your company.";
} else if(!$yearsbusiness){
$errorMsg .= "Please enter the number of years you've been in business.";
} else if(!$annualrevenue){
$errorMsg .= "Please enter your companies Approximate Annual Revenue.";
} else if(!$industrysector){
$errorMsg .= "Please enter the Industry Sector.";
} else if(!$accounttype){
$errorMsg .= "Please choose a Membership Type.";
} else if(!$preferredcontact){
$errorMsg .= "Please enter your preferred method of contact.";
} else if(!$referralsource){
$errorMsg .= "Please enter the Referral Source.";
} else
// Database duplicate Fields Check
$sql_username_check = mysql_query("SELECT id FROM members WHERE username='$username' LIMIT 1");
$sql_email_check = mysql_query("SELECT id FROM members WHERE email='$email' LIMIT 1");
$username_check = mysql_num_rows($sql_username_check);
$email_check = mysql_num_rows($sql_email_check);
if ($username_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside our system. Please try another.";
} else if ($email_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
} else {
// Add MD5 Hash to the password variable
$hashedPass = md5($password);
// Add user info into the database table, claim your fields then values
$sql = mysql_query("INSERT INTO members (username, email, password, phone, address, city, state, zip, emailactivated, accounttype, lastlogin, signupdate, name, fax, company, website, numemployees, yearsbusiness, annualrevenue, industrysector, preferredcontact, referralsource)
VALUES('$username', '$email', '$password', '$phone', '$address', '$city', '$state', '$zip', '$emailactivated', '$accounttype', '$lastlogin', '$signupdate', '$name', '$fax', '$company', '$website', '$numemployees', '$yearsbusiness', '$annualrevenue', '$industrysector', '$preferredcontact', '$referralsource', now())") or die (mysql_error());
// Get the inserted ID here to use in the activation email
$id = mysql_insert_id();
// Create directory(folder) to hold each user files(pics, MP3s, etc.)
mkdir("memberFiles/$id", 0755);
// Start assembly of Email Member the activation link
$to = "$email";
// Change this to your site admin email
$from = "###############";
$subject = "One Last Step";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Hi ' . $name . ',
<br /><br />
One Last Step before we can review your application.
<br /><br />
Please click here to activate now >>
<a href="http://www.############.com/activation.php?id=' . $id . '">
ACTIVATE NOW</a>
<br /><br />
Your Login Data is as follows:
<br /><br />
E-mail Address: ' . $email . ' <br />
Password: ' . $password . '
<br /><br />
Thanks!
<br /><br />
Houstonians For A Better Tomorrow
</body>
</html>';
// end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
// Then print a message to the browser for the joiner
print "<br /><br /><br /><h4>OK $firstname, one last step to verify your email identity:</h4><br />
We just sent an Activation link to: $email<br /><br />
<strong><font color=\"#990000\">Please check your email inbox in a moment</font></strong> to click on the Activation <br />
Link inside the message. After email activation you can log in.";
exit(); // Exit so the form and page does not display, just this success message
} // Close else after database duplicate field value checks
} // Close else after missing vars check
} //Close if $_POST
?>
<?php include ("header.php"); ?>
</div>
</div>
<?php include ("subhead.php"); ?>
<!-- Content Wrapper -->
<div class="contentWrapper">
<div class="outerShadow">
</div>
<div class="innerShadow">
</div>
<div class="center clearfix">
<!-- Additional clearfix necessary for non floated objects -->
<div class="clearfix">
</div>
<!-- Content Starts - Header template should end here -->
<!--Left layout column -->
<div class="siteColumnLeft">
<div class="column">
<table width="750" align="center" cellpadding="4">
<tr>
<td width="7%">Please complete the entire application. </td>
</tr>
</table>
<table width="600" align="center" cellpadding="5">
<form action="join_form.php" method="post" enctype="multipart/form-data">
<tr>
<td colspan="2"><font color="#FF0000"><?php echo "$errorMsg"; ?></font></td>
</tr>
<tr>
<td width="300"><div align="right">User Name:</div></td>
<td width="450"><input name="username" type="text" value="<?php echo "$username"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right"> Password: </div></td>
<td width="450"><input name="password" type="password" value="<?php echo "$password"; ?>" />
<font size="-2" color="#006600">(letters or numbers only, no spaces no symbols)</font></td>
</tr>
<tr>
<td width="300"><div align="right">Name:</div></td>
<td width="450"><input name="name" type="text" value="<?php echo "$name"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Phone:</div></td>
<td width="450"><input name="phone" type="text" value="<?php echo "$phone"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Fax:</div></td>
<td width="450"><input name="fax" type="text" value="<?php echo "$fax"; ?>" /></td>
</tr>
<tr>
<td width="163"><div align="right">Email:</div></td>
<td width="450"><input name="email" type="text" value="<?php echo "$email"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Address:</div></td>
<td width="450"><input name="address" type="text" value="<?php echo "$address"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">City: </div></td>
<td width="450"><input name="city" type="text" value="<?php echo "$city"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">State: </div></td>
<td width="450"><input name="state" type="text" value="<?php echo "$state"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Zip Code: </div></td>
<td width="450"><input name="zip" type="text" value="<?php echo "$zip"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Company: </div></td>
<td width="450"><input name="company" type="text" value="<?php echo "$company"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Website: </div></td>
<td width="450"><input name="website" type="text" value="<?php echo "$website"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">No. Of Employees: </div></td>
<td width="450"><input name="numemployees" type="text" value="<?php echo "$numemployees"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">How many years have you been in business? </div></td>
<td width="450"><input name="yearsbusiness" type="text" value="<?php echo "$yearsbusiness"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">What are your Approximate Annual Revenues? </div></td>
<td width="450"><input name="annualrevenue" type="text" value="<?php echo "$annualrevenue"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">Industry Sector: </div></td>
<td width="450"><input name="industrysector" type="text" value="<?php echo "$industrysector"; ?>" /></td>
</tr>
<tr>
<td width="300"><div align="right">What level would you like to become a member of Houstonians For A Better Tomorrow? </div></td>
<td width="450"><select name="accounttype">
<option value="<?php echo "$accounttype"; ?>"><?php echo "$accounttype"; ?></option>
<option value="a">Urban Small Business Member</option>
<option value="b">Corporate Member</option>
<option value="c">Non-Profit</option>
</select></td>
</tr>
<tr>
<td width="300"><div align="right">How do you prefer to receive updates? </div></td>
<td width="450"><select name="preferredcontact">
<option value="<?php echo "$preferredcontact"; ?>"><?php echo "$preferredcontact"; ?></option>
<option value="a">Email</option>
<option value="b">Fax</option>
<option value="c">Direct Mail</option>
</select></td>
</tr>
<tr>
<td width="300"><div align="right">How did you find out about Houstonians For A Better Tomorrow?</div></td>
<td width="450"><select name="referralsource">
<option value="<?php echo "$referralsource"; ?>"><?php echo "$referralsource"; ?></option>
<option value="a">Advertising - TV </option>
<option value="b">Advertising - Radio</option>
<option value="c">Advertising - Online</option>
<option value="c">Advertising - Print</option>
<option value="c">Referral</option>
</select></td>
</tr>
<tr>
<td width="300"><div align="right"></div></td>
<td width="450"><input type="submit" name="Submit" value="Submit Form" /></td>
</tr>
</form>
</table>
</div></div>
</div>
</div>
<!-- Twitter Widget -->
<div class="twitterWidget">
<div class="center">
<!-- Simply change the href to your username -->
<a class="profileLink" href="http://twitter.com/##############"></a><p>Loading<span>Retrieving latest tweet...</span></p>
</div>
</div>
<?php include ("footer.php"); ?>
</body></html>
Presuming join_form.php is the current form (use $_SERVER['PHP_SELF'] instead)...
Insert some debugging code so you can follow what is happening: at the top of the document put the following so you can see what is being passes.
var_dump($_POST);
After each `if' statement echo "Here 1" or "Here 2" so you can see where the code is going.
After your ereg_replace() use:
var_dump($username, $address, $state, $city, $accounttype, $email, $password);
Then you can start to debug your problem.
Ok. So this is my code below. I'm trying to follow a tutorial on webtuts about validating email. But my sample is not working out. It is supposed to alert the user that it has entered an invalid email. So what my mate did is he created the "show_warning" jquery function to allow me to display my $msg. But it doesn't work. Is my logic wrong?.
<?php
if(isset($_POST['username']) && !empty($_POST['username']) AND
isset($_POST['password']) && !empty($_POST['password']) AND
isset($_POST['email']) && !empty($_POST['email']) AND
isset($_POST['role_id']) && !empty($_POST['role_id']))
{
$username = ($_POST['username']);
$password = ($_POST['password']);
$email = ($_POST['email']);
$role_id = ($_POST['role_id']);
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$msg = 'The email you entered is invalid. Please try again.';
}
else
{
$msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';
}
}
?>
======================================
<div id="main-content">
<div id="create-user">
<h1>Create User</h1>
<form action="" method="post">
<table id="userform" width="600px" border=0>
<tr>
<td><label for="username">Username</label>
<input type="text" id="username" name="username" /></td>
<td><label for="password">Password</label>
<input type="text" id="password" name="password" /></td>
</tr>
<tr>
<td><label for="email">Email</label>
<input type="text" id="email" name="email" /></td>
<td><label for="role_id">Role</label>
<select>
<?php $roles = load_roles() ;?>
<?php foreach ($roles as $role): ?>
<option value='<?php echo $role['role_id']; ?>'><?php echo $role['role']; ?></option>
<?php endforeach; ?>
</select></td>
</tr>
<tr>
<td><input type="submit" id="submit" name="save_user" value="Create User"></td>
</tr>
</table>
</form>
</div>
</div>
for validating email php provides
$email="test#gmail.com" //your email to validate here
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "E-mail is valid";
}
else
{
echo "E-mail is not valid";
}
and you must not use eregi. you can use preg_match()
for more validation function follow this link
http://php.net/manual/en/filter.filters.validate.php
eregi() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.
so its would be actual reason
AND preg_match() with the i (PCRE_CASELESS) modifier is the suggested alternative.
as in answer of Yadav Chetan use FILTER_VALIDATE_EMAIL instead those regx
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
echo $msg = 'The email you entered is invalid. Please try again.';
}
else
{
echo $msg = 'Your account has been made, <br /> please verify by clicking the activation link in your email.';
}
Forgive my noobness coding as I teach myself PHP (lol)!
The code below works almost perfectly for a form I want to create. Whenever the user hits the submit button, provided the form is all filled in correctly, the text "E-mail Sent Successfully" message appears at the top of the page. If any of the mandatory fields are incomplete or invalid, then an error message is returned underneath the corresponding input field and the "e-mail sent successfully" message does not appear. This works properly for all but the last if statement (antispam). Essentially, if you enter the correct answer (3) into the antispam box, then regardless of the other fields being filled in or not, the "e-mail Sent Successfully" message appears (it shouldn't do this as other fields are not filled out correctly). The "e-mail Sent Successfully" message should only appear if all the mandatory fields are completed correctly.
Whatever 'if statement' goes at the bottom adhere's to the same rule (i.e. if name was at the bottom of the if statements, then only name would need to be entered correctly for the e-mail sent successfully message to appear). It feels as though if the last if statement is passed as true, the other statements don't work.
<?php
if (isset($_POST['submit'])) {
$yourname = $_POST['yourname'];
$organisation = $_POST['organisation'];
$email_from = $_POST['email'];
$comments = $_POST['comments'];
$antispam = $_POST['antispam'];
$namestring = "/^[A-Za-z .'-]+$/";
$emailstring = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
if(!preg_match($namestring,$yourname)) {
$error_message_name .= "Please enter a valid name";
}
if(!preg_match($emailstring,$email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
}
if(strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
}
if($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
}
else {
echo "E-mail Sent Successfully";
}
}
else {
$yourname="";
$organisation="";
$email_from="";
$comments="";
$antispam="";
$error_message_email = "";
$error_message_name = "";
$error_message_message = "";
$error_message_antispam = "";
}
?>
<form name="htmlform" method="post" action="ifandelse.php">
<table width="650px">
<tr>
<td>
<label for="yourname"><font color="#ff0000">*</font>Name:</label>
</td>
<td>
<input type="text" name="yourname" value="<?PHP echo$yourname;?>" maxlength="50" size="40"><br>
<div class="warning">
<?php print $error_message_name;?>
</div>
</td>
</tr>
<tr>
<td>
<label for="organisation">Organisation:</label>
</td>
<td>
<input type="text" name="organisation" value="<?PHP echo$organisation;?>"maxlength="50" size="40">
</td>
</tr>
<tr>
<td>
<label for="email"><font color="#ff0000">*</font>E-mail Address:</label>
</td>
<td>
<input type="text" name="email" value="<?PHP echo$email_from;?>" maxlength="80" size="40"><br>
<div class="warning">
<?php print $error_message_email;?>
</div>
</td>
</tr>
<tr>
<td valign="top">
<label for="comments"><font color="#ff0000">*</font>Message:<br></label>
</td>
<td>
<textarea name="comments" rows="8" cols="60"><?PHP echo$comments;?></textarea><br>
<div class="warning">
<?php print $error_message_message;?>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<font color="#ff0000">*</font>To avoid unwanted spam, please answer the following question correctly: 2 + 1 = <input type="text" name="antispam" value="<?PHP echo$antispam?>" maxlength="100" style="width:30px"><br>
<div class="warning">
<?php print $error_message_antispam;?>
</div>
</td>
</tr>
<tr>
<td>
</td>
<td style="text-align:center;">
<input type="submit" class="formsubmit" name="submit" value="Submit Form">
</td>
</tr>
</table>
</form>
Be most grateful for any help, I've had several friends look into this and we still can't figure out why it's doing this. Have tried nesting the 'if statements' differently, changing the if statement's to else if, etc with no joy :(
I've looked on the forums and can't find a solution that works for me.
You should use a variable to know if there was an error (or more than one) at the end of your verifications. Some code below to give you the general idea :
$isError = false;
if (!preg_match($namestring, $yourname)) {
$error_message_name .= "Please enter a valid name";
$isError = true;
}
if (!preg_match($emailstring, $email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
$isError = true;
}
if (strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
$isError = true;
}
if ($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
$isError = true;
}
if ($isError) {
//do wathever you have to do
} else {
//send mail
echo "E-mail Sent Successfully";
}
The else statement is attached to you last if and only to this one. Hence, only the last condition matters regarding whether your success message is displayed or not. You'll have to chain your checks :
if (!preg_match($namestring, $yourname)) {
$error_message_name .= "Please enter a valid name";
}
elseif (!preg_match($emailstring, $email_from)) {
$error_message_email .= "Please enter a valid e-mail address";
}
elseif (strlen($comments) < 4) {
$error_message_message .= "Please enter a valid message";
}
elseif ($antispam != "3") {
$error_message_antispam .= "Please enter a valid answer";
} else {
echo "E-mail Sent Successfully";
}
Of course, this will still leave you displaying only one error message at a time, which is far from optimized but that's another matter.
I am trying to make a register form for my website where it asks for a users password once, and when they click submit, a JavaScript box will pop up and ask it from he user again and checks it against the old one and if they are not the same it stays on the join page and tells them passwords don't match. How would I go about doing this?
Here is the HTML document I have:
<h2>New Account Registration.</h2>
<table width="600" cellpadding="1" style="text-align:center;">
<form action="join.php" method="get" >
<tr>
<td colspan="1"><font color="#FF0000"></font></td>
</tr>
<tr>
<td>Company Name:</td>
</tr>
<tr>
<td><input style="background-color: #FFF;" name="companys_name" type="text" placeholder="Google" required /></td>
</tr>
<tr>
<td>First Name:</td>
</tr>
<tr>
<td ><input style="background-color: #FFF;" name="admin_first_name" type="text" placeholder="John" required />
</td>
</tr>
<tr>
<td>Last Name: </td>
</tr>
<tr>
<td>
<input style="background-color: #FFF;" name="admin_last_name" type="text" placeholder="Doe" required />
</td>
</tr>
<tr>
<td>Phone Number:</td>
</tr>
<tr>
<td><input style="background-color: #FFF;" type="tel" name="admin_phone" pattern="[0-9]{3}-?[0-9]{3}-?[0-9]{4}" required placeholder="123-123-1231"></td>
</tr>
<tr>
<td>Website: </td>
</tr>
<tr>
<td><input style="background-color: #FFF;" type="url" id="orderWebsite" placeholder="http://domain.com" /></td>
</tr>
<tr>
<td>Email: </td>
</tr>
<tr>
<td><input style="background-color: #FFF;" type="email" id="orderEmail" placeholder="name#domain.com" required /></td>
</tr>
<tr>
<td> Password: </td>
</tr>
<tr>
<td> <input style="background-color: #FFF;" name="admin_password" type="password" required />
</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Register!" onclick="register()" /></td>
</tr>
</form>
</table>
Here is the PHP page it connects to:
<?php
$errorMsg = "";
// First we check to see if the form has been submitted
if (isset($_POST['companys_name'])){
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Filter the posted variables
$companys_name = preg_replace("[^A-Za-z0-9]", "", $_POST['companys_name']); // filter everything but numbers and letters
$admin_first_name = preg_replace("[^A-Za-z]", "", $_POST['admin_first_name']); // filter everything but letters
$admin_phone = preg_replace("[^0-9]", "", $_POST['admin_phone']);// Filters everything except numbers
$admin_email = preg_replace("[^A-Z a-z0-9]", "", $_POST['admin_email']); // filter everything but spaces, numbers, and letters
$admin_url = preg_replace("[^A-Z a-z0-9]", "", $_POST['admin_url']); // filter everything but spaces, numbers, and letters
$admin_last_name = preg_replace("[^A-Z a-z]", "", $_POST['admin_last_name']); // filter everything but spaces, numbers, and letters
$admin_email = stripslashes($_POST['admin_email']);
$admin_email = strip_tags($admin_email);
$admin_email = mysql_real_escape_string($admin_email);
$admin_password = preg_replace("[^A-Za-z0-9]", "", $_POST['admin_password']); // filter everything but numbers and letters
// Check to see if the user filled all fields with
// the "Required"(*) symbol next to them in the join form
// and print out to them what they have forgotten to put in
if((!$companys_name) || (!$admin_first_name) || (!$admin_phone) || (!$admin_last_name) || (!$admin_email) || (!$admin_password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$companys_name){
$errorMsg .= "--- Company Name";
} else if(!$admin_first_name){
$errorMsg .= "--- Phone Number 10 Digits";
} else if(!$admin_phone){
$errorMsg .= "--- Phone Number 10 Digits";
} else if(!$admin_last_name){
$errorMsg .= "--- Last Name";
} else if(!$admin_email){
$errorMsg .= "--- Email Address";
} else if(!$admin_password){
$errorMsg .= "--- Password";
}
} else {
// Database duplicate Fields Check
$sql_companys_name_check = mysql_query("SELECT id FROM toc_companys WHERE companys_name='$companys_name' LIMIT 1");
$sql_admin_email_check = mysql_query("SELECT id FROM toc_companys WHERE admin_email='$admin_email' LIMIT 1");
$companys_name_check = mysql_num_rows($sql_companys_name_check);
$admin_email_check = mysql_num_rows($sql_admin_email_check);
if ($companys_name_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your company name is already in use inside our system. Please try another.";
} else if ($admin_email_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
} else {
// Add MD5 Hash to the password variable
//$hashedPass = md5($password);
// Add user info into the database table, claim your fields then values
$sql = mysql_query("INSERT INTO toc_companys (companys_name, admin_first_name, admin_phone, admin_url, admin_last_name, admin_email, admin_password )
VALUES('$companys_name', '$admin_first_name', '$admin_phone','$admin_url','$admin_last_name', '$admin_email','$admin_password', now())") or die (mysql_error());
// Get the inserted ID here to use in the activation email
$id = mysql_insert_id();
// Create directory(folder) to hold each user files(pics, MP3s, etc.)
mkdir("memberFiles/$id", 0755);
// Start assembly of Email Member the activation link
$to = "$admin_email";
// Change this to your site admin email
$from = "admin#5mutny.com";
$subject = "Activate your account!";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Hi ' . $admin_first_name . ' at ' . $companys_name . ',
<br /><br />
You must complete this step to activate your account with us.
<br /><br />
Please click here to activate now >>
<a href="http://www.testsite.com/activation.php?id=' . $id . '">
ACTIVATE NOW</a>
<br /><br />
Your Login Data is as follows:
<br /><br />
E-mail Address: ' . $admin_email . ' <br />
Password: ' . $admin_password . '
<br /><br />
Thanks!
</body>
</html>';
// end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
// Then print a message to the browser for the joiner
print "<br /><br /><br /><h4>OK $firstname, one last step to verify your email identity:</h4><br />
We just sent an Activation link to: $admin_email<br /><br />
<strong><font color=\"#990000\">Please check your email inbox in a moment</font></strong> to click on the Activation <br />
Link inside the message. After email activation you can log in.";
exit(); // Exit so the form and page does not display, just this success message
} // Close else after database duplicate field value checks
} // Close else after missing vars check
} //Close if $_POST
?>
My teacher wants me to make it so when someone clicks register, a JavaScript box will pop up asking the user to retype their password so that it will check vs. the inputbox one and if they are the same it will make their account if not it won't leave the page but it will tell them their passwords are not the same.
Pseudocode via JQuery:
$('#submit').click(function(){
$('#box').show();
});
$('#box > #confirm').click(function(){
var firstInput = $('#firstInput').val();
var secondInput = $('#secondInput').val();
if(!firstInput.equal(secondInput)) {
//do something if they are not the same
}
});
set id for your form, and add a box:
<form id="myform" action="join.php" method="get" >
...
</form>
<div id="box" style="display: none; width: 100px; height: 100px; backgournd-color: #FFFFFF;">
<input id="confirmPassword"/>
<span id="info"></span>
<button id="confirm">confirm</button>
</div
add script in the bottom of your .html
$('#myform').submit(function(){
$('#box').show();
return false; // cancel the submit action
});
$('confirm').click(function(){
if($('input[value="admin_password"]').val().equal($('#confirmPassword').val())) {
$('#myform').submit(); // to submit the form here
} else {
$('#info').text("passwords are not the same!").show().fadeOut(1000);
}
});
PHP contact form phone validation of the correct amount of numbers
Hello,
I have this php form that validates the content once submitted a sticky php form is what it is called. It keeps the users data in the input box when an error if found so the user dose not have to re-enter all the data again.
When the phone number is submitted I need it to validate that there are 3 characters/numbers in the first input box then 3 in the next then 4 in the last one.
The way it is now as long as you input numbers in the first input box it over looks the rest of the input boxes for the phone number. So I am looking to add a minimum character/number script in the validation process. I have the form validating that it is a number at this time. I also need it to validate that there is the correct amount of numbers in each input box for the phone as well. I believe this is just changing the elseif statements to just if inside another if but that did not work either. Any help would be very appreciated. The Art Institute only taught so much with PHP, and not this.
This is the particular area of the script that validates the phone number:
//validate the phone number
if(is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}elseif(is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}elseif(is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
This is a copy of the whole script for the form itself:
<?php
// This page receives the data from itself and validates as well
//error reporting!
ini_set ('display_errors', 1);
//Shows all possible problem!
error_reporting (E_ALL);
// validate email
function isValidEmail($email){
return eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $email);
}
//show form
function show_form($firstName='',$lastName='',$businessName='',$email='',$phone01='',$phone02='',$phone03='',$message=''){
?>
<!--The form starts here -->
<form action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact form" target="_self" id="contact form" dir="ltr" >
<table bgcolor="#000000" width="525" border="0" align="center">
<tr>
<td width="25%" align="right">*First Name:</td>
<td colspan="2" align="left"><input name="firstName" type="text" id="firstName" tabindex="1" size="30" value="<?php if(isset($_POST['firstName'])) { print htmlspecialchars($_POST['firstName']); }?>"/></td>
</tr>
<tr>
<td align="right">*Last Name:</td>
<td colspan="2" align="left"><input name="lastName" type="text" id="lastName" tabindex="2" size="30" value="<?php if(isset($_POST['lastName'])) {print htmlspecialchars($_POST['lastName']); }?>"/></td>
</tr>
<tr>
<td align="right">Business Name:</td>
<td colspan="2" align="left"><input name="businessName" type="text" id="businessName" tabindex="3" size="35" value="<?php if(isset($_POST['businessName'])) {print htmlspecialchars($_POST['businessName']); }?>"/></td>
</tr>
<tr>
<td align="right">*Email: </td>
<td colspan="2" align="left"><input name="email" type="text" id="email" tabindex="4" size="35" value="<?php if(isset($_POST['email'])) {print htmlspecialchars($_POST['email']); }?>"/></td>
</tr>
<tr>
<td align="right">*Phone Number:</td>
<td colspan="2" align="left">
<input name="phone01" type="text" id="phone01" size="3" maxlength="3" tabindex="5"value="<?php if(isset($_POST['phone01'])) {print htmlspecialchars($_POST['phone01']); }?>"/>
- <input name="phone02" type="text" id="phone02" size="3" maxlength="3" tabindex="6"value="<?php if(isset($_POST['phone02'])) {print htmlspecialchars($_POST['phone02']); }?>"/>
- <input name="phone03" type="text" id="phone03" size="4" maxlength="4" tabindex="7" value="<?php if(isset($_POST['phone03'])) {print htmlspecialchars($_POST['phone03']); }?>"/></td>
</tr>
<tr align="center">
<td align="right">*Message:</td>
<td colspan="2" align="left"><textarea name="message" type="text" id="message" tabindex="8" cols="45" rows="4"><?php if(isset($_POST['message'])) {print htmlspecialchars($_POST['message']); }?></textarea>
</td>
</tr>
<tr align="center">
<td> </td>
<td><input name="submit" type="submit" tabindex="9" value="Email" /></td>
<td><input type="reset" name="reset" id="reset" value=" Reset " tabindex="10"/></td>
</tr>
</table>
</form>
<?php
} // end of show_form function
$validate = TRUE;
if($_SERVER['REQUEST_METHOD']!='POST') {
show_form();
} else {
//validate form fields
//validate the first name
if(empty($_POST['firstName'])) {
print '<p class="error">Please enter your First Name.</p>';
$validate = FALSE;
}
//validate the last name
if(empty($_POST['lastName'])) {
print '<p class="error">Please enter your Last Name.</p>';
$validate = FALSE;
}
//validate the enail with email arrary
if(!isValidEmail($_POST['email'])) {
print '<p class="error">Please enter your Email Address in the correct formate.</p>';
$validate = FALSE;
}
//validate the phone number
if(is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}elseif(is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}elseif(is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
//validate the message
if(empty($_POST['message'])) {
print '<p class="error">Please enter your Messagee.</p>';
$validate = FALSE;
}
if(!$validate){
print "<p>Please fill in all the fields with an asterisk * next to it and than please try again!</p>";
show_form($_POST['firstName'],$_POST['lastName'],$_POST['businessName'],$_POST['email'],$_POST['phone01'],$_POST['phone02'],$_POST['phone03'],$_POST['message']);
}else{
$phone01 = $_POST['phone01'];
$phone02 = $_POST['phone02'];
$phone03 = $_POST['phone03'];
$phone = $phone01.'-'.$phone02.'-'.$phone03;
//confirmation email to client includes all information provided
mail($_POST['email'], 'Contact Confirmation from www.Ozbar.net Web site', 'Thank You '.$_POST['firstName'].' '.$_POST['lastName'].' for your request for us to contact you.
Below is the information your provided us to contact you per your request.
First Name: '.$_POST['firstName'].'
Last Name: '.$_POST['lastName'].'
Business Name: '.$_POST['businessName'].'
Email Address: '.$_POST['email'].'
Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].'
Message: '.$_POST['message'].' ','From:contact#steveoatman.me);
//notice of a new contact request
mail('contact#steveoatman.me, 'Contact Request from www.Steveoatman.me Web site', '
First Name: '.$_POST['firstName'].'
Last Word: '.$_POST['lastName'].'
Business Name: '.$_POST['businessName'].'
Email Address: '.$_POST['email'].'
Phone Number: '.$_POST['phone01'].'-'.$_POST['phone02'].'-'.$_POST['phone01'].'
Message: '.$_POST['message'].' ','From:contact#steveoatman.me);
print '<p align="center">Thank You For Your Request!</p>'?><br /><?php
print '<p align="center">We will contact you back with in 24-48 hours.</p>'
?>
<br /><br /> <!-- if all validated a thank you statement -->
<?php
}
} //end of IF submit
// end of all php
?>
<!-- end of #ref form -->
Use strlen to validate the field lengths. Do not use if/elseif as you want to verify all three inputs. Set a flag to keep track of the validity of the phone number.
$invalid_phone = false;
if((strlen($_POST['phone01']) == 3) && is_numeric($_POST['phone01'])) {
$phone = $_POST['phone01']. '-';
}else{
$invalid_phone = true;
}
if((strlen($_POST['phone02']) == 3) && is_numeric($_POST['phone02'])) {
$phone .= $_POST['phone02']. '-';
}else{
$invalid_phone = true;
}
if((strlen($_POST['phone03']) == 4) && is_numeric($_POST['phone03'])) {
$phone .= $_POST['phone03'];
}else{
$invalid_phone = true;
}
if($invalid_phone){
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
The code above is just checking whether any of the 3 fields have a number in them, rather than all of them.
To achieve what you are going for above, something like this would do it:
if (is_numeric($_POST['phone01']) && is_numeric($_POST['phone02']) && is_numeric($_POST['phone03']))
{
$phone = $_POST['phone01']."-".$_POST['phone02']."-".$_POST['phone03'];
}
else
{
print '<p class="error">Please enter your Phone Number as 10 Number.</p>';
$validate = FALSE;
}
However, the above code does not do any other kind of validation, such as checking to see that the required number of digits have been put in each form field.
You might also want to use the 'ctype_digit()' function to make sure that only digits are entered, rather than a numric string such as 1.3.
So you could do something like
if (!ctype_digit($_POST['phone01']) || strlen($_POST['phone01']) != 4)
{
$validate = FALSE;
}