0

Greetings for the day,

I am new to php and my company gave me a task of registration form, i want to know how to validate email address using php(not with JavaScript) like there will must a one . and @ sign i have done t using JavaScript but TL said that it must be using PHP. My Html code is given below can any one help??

<html>
<head><title>Form validation</title>
    <script type="text/javascript">
/*function validateEmail() {
    var emailText = document.getElementById('email').value;
    var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
    if (pattern.test(emailText)) {
        return true;
    } else {
        alert('Bad email address: ' + emailText);
        return false;
    }
}

window.onload = function() {
    document.getElementById('email_form').onsubmit = validateEmail;
}
*/
</script>


</head>
<body>

<form action="<?php echo base_url();?>index.php/validation_control/insert_to_db"  method="post" id="email_form" onsumbit="return myFunction()">
<table align="center" border="2">
<tr><td>Username :- <td><input type="text" name='uname'/></td></td></tr>
<tr><td>First Name :- <td><input type="text" name='fname'/></td></td></tr>
<tr><td>Last Name :- <td><input type="text" name='lname'/></td></td></tr>
<tr><td>Email :- <td><input type="text" name='email' /><?php //echo $emailErr;?></td></td></tr>
<tr><td>Password :-<td><input type="password" name='pass' id='pass'/></td></td></tr>
<tr><td>Confirm Password :-<td><input type="password" name='cnfpass' id='cnfpass'/></td></td></tr>
<tr><td>Status :-<td><input type="radio" name="status" value="active" />Active<input type="radio" name="status" value="deactive" />Deactive<input type="radio" name="status" value="delete" />Delete</td></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="sbmt" id="sbmt" value="Submit" onclick="Javscript:validateEmail"/></td></tr>


</table>
</form>
</body>
</html>
Ritesh
  • 650
  • 1
  • 12
  • 23

5 Answers5

3

Edit

Your company is asking you to do the address validation on the server. Sometimes you want to do it in both places, ie:

  • On the client: To provide instant feedback about whether input is valid before submitting
  • On the server: To validate the input in case some bad input got through

Your form data is submitted to the server via a POST request (usually), so the PHP endpoint that handles the request must get the form field from the request and validate it.

For instance, you might "get" the email address by doing:

$emailAddress = $_POST["email"];

The $_POST array is specific to each request and has all of the form data in a dictionary (well really an array I think, but whatever). In general, you can get the form data by doing $_POST["the_input_id"] where the_input_id is the form element's input ID.

Once you get the stuff you need, then you can do the validation.

PHP Side

See Using a regular expression to validate an email address to find a good RegEx to validate your email addresses.

There is no "canonical" RegEx that I know of for email addresses as different servers allow different sorts of addresses. The RFC for what actually is a valid email address is not uniformly followed.

For instance, a very intense RegEx is available at http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html.

Then use php's built-in RegEx functions, ie:

$EmailRegEx = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
$InputAddress = "tacos_tacos_tacos@nowhere.com";

$EmailAddressValid = preg_match($EmailRegEx, $InputAddress);

if ($EmailAddressValid) {
    $this->_SendMe();
}

See http://www.developphp.com/view_lesson.php?v=224 for a simple example.

Community
  • 1
  • 1
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
  • Plz give me a proper code or do editing in my code. I have done googling and try so many function but they are not working so plz do editing in my code and paste it here – Ritesh Dec 13 '13 at 06:57
  • 1
    People on StackOverflow are super generous and very eager to help you but you must (among other things) 1) clearly explain your problem, 2) show what you have tried to do already and the (not working) result, 3) ask a constructive question about what should be done / the underlying reason why it is not working. I am pretty lenient about these but the more serious SO users do not take kindly to asking for providing code in this way. – tacos_tacos_tacos Dec 13 '13 at 06:59
  • i paste my code above plz see that wat i have done. i hd done using this thing javascript and it is working properly but now i have to do same thing using php. @tacos_tacos_tacos – Ritesh Dec 13 '13 at 07:01
  • @tacos_tacos_tacos +1 for your answer and also for your comment. and ritesh if somebody already helping you than dont ask or extra favour :) – R R Dec 13 '13 at 07:03
  • @Ritesh good luck my friend. – tacos_tacos_tacos Dec 13 '13 at 07:07
  • 1
    @Ritesh thats rude :) but still see how people are putting extra effort and their time to help their co programmers in stack overflow,i think u sholud be thankful instead of complaining. – R R Dec 13 '13 at 07:08
0
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
     //Valid email!
}

You can see more in the documentation

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0

Try this

<input type="email">

Hope its simple

0

 include 'connect.php'; 



require 'class.phpmailer.php';
    $mail = new PHPMailer();

    // ---------- adjust these lines ---------------------------------------
    $mail->Username = "email"; // your GMail user name
    $mail->Password = "passwd"; // your GMail password
    $mail->AddAddress($_POST['t5']);
    $eml1=$_POST['t5'];
    //$ky=$_POST[$key];
    $mail-> AddReplyTo ='';
    $mail->ReturnPath='';

    $mail->FromName = ''; // sender name

     $mail->Subject = '';
     $sql="SELECT * FROM $tbl_name WHERE emailid='$eml1'";
     $result=mysql_query($sql);

     $row = mysql_fetch_array($result);
     $key=$row['randomkey'];
     $mail->Body = "Click to this link for conformation login Account http://domain/passwd.php?evalue=$eml1&rdk=$key";   

    //-----------------------------------------------------------------------

    $mail->Host = "ssl://smtp.gmail.com"; // GMail
    $mail->Port = 465;
    $mail->IsSMTP(); // use SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->From = $mail->Username;
    if(!$mail->Send())
        echo "Mailer Error: " . $mail->ErrorInfo;
    else
       //   header("Location: contact.php");
      echo 'Message was Sucessfully Sent  ';
      echo 'Check Your Email Address Click to Link and Create New Password';


      echo 'your hyperlink for redirect ';


0

You can use regular expression to validate email address

$email = "youremail@yahoo.com";
define( 'EMAIL_REGEX',
        '^(([0-9A-Za-z]|[0-9A-Za-z][0-9a-zA-Z_\.-]+)@'.__DOMAIN_REGEX__.')$' );

if ( preg_match( '/'.EMAIL_REGEX.'/', $email ) ) {
    echo 'Email Adress is valid';
} else {
    echo 'Email Adress is not valid';
}

alternative solution is to use php filter_var

zai
  • 75
  • 9
  • Thank u where to put this code & how to implement it in my code?? – Ritesh Dec 13 '13 at 07:43
  • just put between php tag bro! :) Check this [php tutorial](http://www.tizag.com/phpT/). Good for php beginners – zai Dec 13 '13 at 08:05