Dynadot โ€” .com Registration $8.99

Question about submitting data using HTML forms

Spaceship Spaceship
Watch
Impact
0
Dear All

I am new to webdesign and I would like to ask a couple of question about the usage of forms on a web-page.

1)What is the best way to confirm that the user has entered a valid email address?

2)Can all the data entered in a form be mailed directly to an email id using the action="mailto:[email protected]" option?

With Regards
Sachin Aggarwal
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
Code:
// checks to see if email address was entered in correct format.
	if (!preg_match('|[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*[a-z]+|', $_POST['email']))
	// if not, add that error to our array
	$errors[] = "Invalid email address.";

Something like that. Use an array.
 
0
•••
2) Yes, but it'd be very easy for spambots to pick up your email. And you can't check for valid email address etc.

And Richy if you don't mind I'd like to turn that into a function

PHP:
function check_email($email) {
//usage:
// if(check_email("[email protected]")) { //continue } else { //errors }
if (!preg_match('|[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*[a-z]+|', $email)) {
 return false;
} else {
 return true;
}
}
 
0
•••
Here is what you would need:

The Form:
Code:
<form action="email.php">
Email: <input type="text" name="email"/><br/>
Message: <textarea name="message"></textarea><br/>
<input type="submit" value="Send Email"/>
</form>

Email.php
PHP:
<?php
function check_email($email){ 
     if(!preg_match('|[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*[a-z]+|', $email)){ 
          return false; 
     }else{ 
          return true; 
     } 
}

if(check_email($_POST['email'])==true){
     mail('Your Email Address Here', 'Subject Here', $_POST['message'], 'Reply-to: '.$_POST['email']);
     echo 'Email Sent';
}else{
     die('Invalid Email');
}
?>
 
0
•••
PHP:
/**
* Checks an email for valid format.
*
* @param  string  Email address
* @return boolean
*/
function is_valid_email($email)
{
	return (bool)(preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s\'"<>]+\.+[a-z]{2,6}))$#si', $email));
}

:)
 
0
•••
Below is the code for email address validation in javascript.

Javascript Code :

Code:
<script language = "Javascript">
function echeck(str) {

        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr= str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1){
           alert("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(at)==-1 || str.indexOf (at)==0 || str.indexOf(at)==lstr){
           alert("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            alert("Invalid E-mail ID") 
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
            alert("Invalid E-mail ID")
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            alert("Invalid E-mail ID")
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
            alert("Invalid E-mail ID") 
            return false
         }
        
         if (str.indexOf(" ")!=-1){
            alert("Invalid E-mail ID")
            return false
         }

          return true                     
    }

function ValidateForm(){
    var emailID=document.frmSample.txtEmail
    
    if ((emailID.value==null)||(emailID.value=="")){
        alert("Please Enter your Email ID")
        emailID.focus()
        return false
    }
    if (echeck(emailID.value)==false){
        emailID.value=""
        emailID.focus()
        return false
    }
    return true
 } 
</script>

The JavaScript has two functions:
โ€ข Function echeck is used to verify if the given value is a possible valid email address. This function thus simply makes sure the email address has one (@), atleast one (.). It also makes sure that there are no spaces, extra '@'s or a (.) just before or after the @ and also makes sure that there is atleast one (.) after the @.
โ€ข Function ValidateForm is used to make sure that the email field is not blank and that it is a valid email address on form submission.
Use the above code along with HTML by calling the above functions.

I hope my comments below the code help you understand how it works.
 
0
•••
Technically to make sure the email is valid (or at least the domain is) you should also do an MX lookup on the email domain to see if there actually is a mailserver behind it.
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back