JavaScript syntax

This is a discussion on JavaScript syntax within the A Brief History of Cprogramming.com forums, part of the Community Boards category; OK, so I ran across the following code (in someone's webpage) that checks whether an email address submitted is actually ...

  1. #1
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597

    JavaScript syntax

    OK, so I ran across the following code (in someone's webpage) that checks whether an email address submitted is actually valid (in syntax only, my guess).
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function checkEmail(myForm) {
      if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value)){
        return (true)
      }
      alert("Invalid E-mail Address! Please re-enter.")
      return (false)
    }
    //  End -->
    </script>
    Could someone go through the line if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value)) end explain the syntax (what checks are made)? For example, what does "/^\w" accomplish?
    This is my signature. Remind me to change it.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    To a normal person I bet it looks like a lot of BS.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Looks like a regular expression. I know they exist, they have form like this and they are a lot used for pattern matching, but that's all I know about them.

    Try searching for regular expressions in JavaScript using a search engine. A few sites I've found.
    http://www.johnrobertmorris.com/dev/Regex.asp
    http://www.edevcafe.com/viewdoc.php?eid=98

    From the first site, / seems to be the start of the expression. Then ^ indicates that the pattern matching should start from the beginning, then \w indicates that any alphanumeric character is valid. So a valid e-mail address should always start with an alphanumeric character.

  4. #4
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Basicly it says
    match some ammount of letters, then a @ , then some other letters, then a ., then 3 more letters.

    the"[/.w+]" type things are disqualifiers. Making sure there arnt illegal characters lie /, ^, $& or white spaces in the email address.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
    
    ^ - matches the beginning of the line
    \w+ - matches a word character 1 or more times
    ([\.-]? - matches . or - 0 or 1 times
    \w+)* - matches any word character 1 or more times
    The * matches the entire expression in parens 0 or more times
    @ matches the @ character exactly once
    \w+([\.-]?\w+)* - does exactly what it did before
    (\.\w{2,3})+ - matches a . followed by 2 to 3 word characters 1 or more times
    $ - matches the end of the line
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    so would that script be basically used for checking if the email adress is the same in two different text fields?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No, it just makes sure that the e-mail address follows a specific format. i.e.:
    abc is not valid,
    a@b.com is valid,
    a-@b.com is not,
    a-b@c.com is, etc...

    Its just for matching patterns in a string.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    why is the matching code so confusing then? I've done javascript before, and actually something quite similar to the above code, but the syntax was a lot different.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You'll find regular expressions in many languages. JavaScript just happens to implement them. The thing is, most other ways of pattern matching are language or library specific. A regular expression is just a string which defines the pattern that you're looking for, and then whatever function gets passed the string (or however it is implemented) has to use its own language/library specific methods to find that pattern. In JavaScript, it appears that regular expressions are in fact objects in themselves. The syntax is a bit odd though (the /reg-exp/.test( ) thing looks a bit strange to me).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    I took some time to study the code and your explanation of it, and I guess it does make some logical sense...yet I don't see writting it without a ready reference...even if one coded in JScript all the time, things of that nature would be impossible to remember.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    what exactly do regular expressions offer, anyway? does it just save space/time when coding?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yeah, saving time and space is one thing. Also, they are fairly standard. If you use them from time to time, it can be nice to actually write out the pattern instead of messing with functions and objects.

    I certainly can't do too much with them without reference (I can do some basic things, but it always helps to have a table of special characters in front of me). The place I have seen them used most is in Perl code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Originally posted by Shiro
    ...

    Try searching for regular expressions in JavaScript using a search engine. A few sites I've found.
    http://www.johnrobertmorris.com/dev/Regex.asp
    http://www.edevcafe.com/viewdoc.php?eid=98

    ...
    Thanks, that's exactly what I needed. I knew what the code did, just wanted to know what the syntax in the expression meant. Thanks
    This is my signature. Remind me to change it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21