1

I have the following code as part of my email validation script. I'd like to learn more about the variable reg but don't know how to find relevant information because I do not know what the syntax is called. Could someone direct me to the proper resource or tell me the name of this type of syntax?

function validate(form_id,email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {
      alert('Invalid Email Address');
      return false;
   }
}
Yahel
  • 37,023
  • 22
  • 103
  • 153
Simon Suh
  • 10,599
  • 25
  • 86
  • 110
  • Also, your regex is very wrong. See http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/532972#532972. – Mechanical snail Oct 10 '11 at 22:32

6 Answers6

5

It's called a regular expression.

There are a lot of resources on regexes, and particularly about regexes in JS. Here is a guide that explains how to use them:

http://www.javascriptkit.com/javatutors/re.shtml

and a guide to the patterns themselves:

http://www.javascriptkit.com/javatutors/redev2.shtml

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
4

Check out the nice wikipedia article:


In computing, a regular expression, also referred to as regex or regexp, provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. A regular expression is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

function check_email() {

//          /^[+a-zA-Z0-9]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i    this anather  regular experession
var pattern = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);

if (pattern.test($('#form_email').val()))
{
    $('#email_error_message').hide();
}
else
{
    $('#email_error_message').html('Invalid Email');
    $('#email_error_message').show();
    error_email = true;

}

}

1

If you are worry about something, thats nothing to worry about. Thats called Regular Expression AKA Regex. What this script is doing in this code? It is matching/validating the user input to only accept email addresses which are well formatted.

test@@@test.com (invalid) test@test.com (valid)

Ismail
  • 634
  • 1
  • 5
  • 11
0

This is a regular expression (regex). Find more information here: http://www.regular-expressions.info/javascript.html.

Starkey
  • 9,673
  • 6
  • 31
  • 51
0

The expression to the right is called a regular expression. You can get more info from : http://www.regular-expressions.info/javascript.html

Chandu
  • 81,493
  • 19
  • 133
  • 134