p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 (http://p2p.wrox.com/forumdisplay.php?f=174)
-   -   RegEx Example question (http://p2p.wrox.com/showthread.php?t=72788)

williamjwayne February 17th, 2009 04:51 PM

RegEx Example question
 
Hi:

I am referring to the example on p.222 at the very bottom (the two alerts). The books says that the isValidEmail function can be called using the alerts. I don't understand how that works. It seems to imply this function can be called by inserting the alerts. I don't see how that would work. What am I missing here. Any help is appreciated.[:confused:]

zeronexxx February 17th, 2009 06:34 PM

hi

i am afriad i don't have the book but if you post more details i will be able to help you .

Thanks

Old Pedant February 17th, 2009 09:12 PM

Agree w/ zeronexxx, but I *suspect* they simply mean you can do
Code:

if ( ! isValidEmail( someFormField ) ) alert("Doofus...I wanted your email address");
But you'll almost always get more responses (and correct responses) if you show the actual code so that those without the book can understand your question.

zeronexxx February 17th, 2009 09:56 PM

or may be something like


PHP Code:


function isValidEmail(EmailField){
    var 
regex = /[a=zA-Z0-9_\-]+@[a-zA-Z0-9\-.]/;
    if(
regex.match(EmailField.value)){
       return 
"this is valid email";
    }else{
       return 
"this email is invalid";
    }
}


alert(isValidEmail(someEmailField)); 


Old Pedant February 17th, 2009 10:07 PM

Good guess!

Of course, it's weird if either of us is right, because then how does the validation cancel the form submittal???

Ugh.

williamjwayne February 18th, 2009 02:46 PM

Followup to my RegEx question ...
 
Thank you all for the responses!

My regrets for not listing all information. The information I was questioning is located on p.222 of JavaScript for Web Developers, Nicholas C. Zakas. In it the author discusses validating e-mail addresses using RegEx techniques. He discusses the major email formats and offers a RegEx validation solution:

function isValidEmail(sText) {
var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
return reEmail.test(sText);
}

He then goes on to add in the next line:

"This function can be called like so:


alert(john.doe@somewhere.com); //outputs "true"

alert(john.doe@somewhere.); //outputs "false" "

(The underlining on the email addresses are not in the book but were added by default in this editor.)

And that is where my confusion comes in. I am unaware of how the function can be called given the code as laid out in the book. Of course the RegEx works but I don't see how the function can be invoked using just an alert. I thought it was just a typo but the errata pages for the book does not address it. Do you have any info that can shed light?[:confused:]

Regards,
Bill Wayne

zeronexxx February 18th, 2009 04:08 PM

Hi Bill

I am afraid that what you have expressed above clearly is not the case, following is the actual code. I downloaded the code and here is the full code.

PHP Code:

<html>
    <
head>
        <
title>E-mail Example</title>
        <
script type="text/javascript">
    

                function 
isValidEmail(sText) {
                    var 
reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
                    return 
reEmail.test(sText);
                }

                
                function 
validate() {
                    var 
oInput1 document.getElementById("txt1");
                    if (
isValidEmail(oInput1.value)) {
                        
alert("Valid");
                    } else {
                        
alert("Invalid!");
                    }

                }
        
</script>
    </head>
    <body>
        <p>E-mail Address: <input type="text" id="txt1" /><br />
        <input type="button" value="Validate" onclick="validate()" /></p>
       
    </body>
</html> 

Hopefully the confusion is clear now?

Regards

williamjwayne February 18th, 2009 09:57 PM

Thanks for the info regarding the errata correction -- I had seen that so I guess my question has been answered. I just wanted to make sure that there was not something that I was missing.

Regards,
Bill Wayne


All times are GMT -4. The time now is 03:57 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.