0

I am new to developing sites, I have created a form with a persons contact details.

In the email text field I would like to only allow email addresses with the character @, if the '@' character is not present, then an error message will be displayed.

Html code for email entry is:

<td class="content"><input name="txtEmail" type="text" class="box" id="txtEmail" size="60" maxlength="100" /></td>

Javascript to ensure that the field is entered is:

function checkShippingAndPaymentInfo()
{
    with (window.document.frmCheckout) {
        if (isEmpty(txtEmail, 'Please enter your email address')) {
            return false;
        } else {
            return true;
        }
    }
}

The code above works but does not have the restriction I require. All I need is validation for the '@' character to be set in the text entry field. Please can someone help me with this.

Thank you in advance.

Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
JUM
  • 61
  • 2
  • 8

2 Answers2

1

You should be able to validate email address using regular expression in Javascript. It will validate the presence of @ along with other email qualifiers. I use the following snippet from this stackoverflow question

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 
Community
  • 1
  • 1
pacman
  • 1,061
  • 1
  • 17
  • 36
  • 1
    by far the best way, there is a lot more to validating an email then just looking for the @ symbol – Gordnfreeman Jun 12 '12 at 19:36
  • 1
    Email address checking is *very* tricky, and any method will pass incorrect addresses or reject correct addresses, usually both. The question was specifically about checking the presence of “@”, which is simplistic but in many ways useful. – Jukka K. Korpela Jun 12 '12 at 19:43
  • So in my case would it be----------- function validateEmail(txtEmail) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA -Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(txEmail); } – JUM Jun 12 '12 at 20:16
  • 1
    @JUM Yes. Make sure your return statement also has "txtEmail" and not "txEmail" – pacman Jun 12 '12 at 21:09
0

Replace type="text" to type="email"

JSA986
  • 5,870
  • 9
  • 45
  • 91
  • Even if your answer is correct, it's always better to briefly explain to OP why you think it could solve its problem. – AFract Aug 24 '15 at 19:56