469,272 Members | 1,482 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,272 developers. It's quick & easy.

javascript form validation

Hi every one, newbie here, so i big hello from me now down to business
I am a web developer but i don't use java script very often, but i have created a form validation from a group of different tutorial, but have run in to a brick wall trying to create a function to compare the email addresses and validate that they are the same because of the combination of different tutorial i have used.
here is the code the function is called "emailval" if anyone with a better knowledge of javascript than me could give me a head up on what missing i would be most grateful.

Expand|Select|Wrap|Line Numbers
  1.  function emailvalidation(entered, alertbox) 
  2. {
  3.  
  4. with (entered)
  5. {
  6. apos=value.indexOf("@");
  7. dotpos=value.lastIndexOf(".");
  8. lastpos=value.length-1;
  9. if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
  10. {if (alertbox) {alert(alertbox);} return false;}
  11. else {return true;}
  12. }
  13.  
  14. function emptyvalidation(entered, alertbox)
  15. {
  16.  
  17. with (entered)
  18. {
  19. if (value==null || value=="")
  20. {if (alertbox!="") {alert(alertbox);} return false;}
  21. else {return true;}
  22. }
  23.  
  24. function emailval(thisform)
  25. {
  26.  
  27. if (entered.value != second.value)
  28. {if (alertbox) {alert(alertbox);} return false;}
  29. else {return true;}
  30.  
  31.  
  32.  
  33.  
  34. function formvalidation(thisform)
  35. {
  36. with (thisform)
  37. {
  38. if (emailval(email,emailtwo,"The two e-mail addresses do not match")==false) {emailtwo.focus(); return false; email.focus(); return false;}; 
  39. if (emailvalidation(email,"Your E-mail Address Is Not Valid")==false) {email.focus(); return false;};
  40. if (emptyvalidation(name,"Please Enter Your Name")==false) {name.focus(); return false;};
  41. if (emptyvalidation(message,"Please Enter Your Equiry")==false) {message.focus(); return false;};
  42. if (emailvalidation(emailtwo,"Your E-mail Address Is Not Valid")==false) {emailtwo.focus(); return false;};
  43.  
  44.  
  45.  
  46.  
  47. }
  48. }
  49.  
May 10 '07 #1
8 1598
iam_clint
1,208 Expert 1GB
if (emailaddy == otheremailaddy) { alert("Congrats they match!"); return true; } else { return false; }
May 10 '07 #2
gits
5,390 Expert Mod 4TB
hi,

yes ... ;) that is much better ... but i think that's the one half of his problem ... he also wants to do a simple validation to have a syntactically correct e-mail address ... meaning somthing like the following:

Expand|Select|Wrap|Line Numbers
  1. part1.part2@part3.domain
think the easiest way should be a regEx ... that matches that ... there are a lot of examples all over the web ... it shouldn't be a problem to find some that you could use for that purpose ... something like

Expand|Select|Wrap|Line Numbers
  1. var foo = 'name.surname@host.domain'; 
  2.  
  3. // the following regEx returns:
  4. // ["name.surname@host.domain", "name", ".", "surname", "@", "host", ".",
  5. // "domain"] into var check
  6.  
  7. var check = foo.match(/(\w+)([.])(\w+)([@])(\w+)([.])(\w+)/);
  8.  
  9. // now you may do something with check ;)
hope that helps ...
May 10 '07 #3
iam_clint
1,208 Expert 1GB
he already has it validating the email address for syntax.
May 10 '07 #4
gits
5,390 Expert Mod 4TB
sure ... he has ... only: our top-level-domain may have 2-4 chars (i.e. info) ... but a regEx is a more common way and you may use the returned array for a lot of things ... if there is a need ... its only an example to show how you may better check such things ... and the shown regEx is not optimal ... i know that. have a look at:

http://www.quirksmode.org/js/mailcheck.html

that is much better ... and has a good explaination too. its really simple to check that way ... you need not to 'extra-check' for emptyness etc. ... was only a hint ... of course you needn't to do it that way ... but if someone is new to javascript you may show him the ways things could be done ... wouldn't you agree? ...
May 10 '07 #5
thank guy here my final solution i am happy to keep the email validation as is i have read the tutorial u recommend and try to implement it unsuccessful see below

Unsuccessful email val code

Expand|Select|Wrap|Line Numbers
  1.  function emailvalidation(entered, alertbox) 
  2. {
  3.     var x = entered.value 
  4.     var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  5.     if (filter.test(x)){return true;}
  6.     {else (alertbox) {alert(alertbox);} return false;}
  7. }
  8.  
  9. final solution
  10.  
  11. function emailvalidation(entered, alertbox)
  12. {
  13.  
  14. with (entered)
  15. {
  16. apos=value.indexOf("@");
  17. dotpos=value.lastIndexOf(".");
  18. lastpos=value.length-1;
  19. if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
  20. {if (alertbox) {alert(alertbox);} return false;}
  21. else {return true;}
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. ///////////////////////
  32. function emptyvalidation(entered, alertbox)
  33. {
  34.  
  35. with (entered)
  36. {
  37. if (value==null || value=="")
  38. {if (alertbox!="") {alert(alertbox);} return false;}
  39. else {return true;}
  40. }
  41. //////////////////////////////
  42. function emailval(entered, enteredtwo, alertbox)
  43. {
  44.  
  45. if (entered.value != enteredtwo.value)
  46. {if (alertbox) {alert(alertbox);} return false;}
  47. else {return true;}
  48.  
  49.  
  50.  
  51. ///////////////////////////////////
  52. function formvalidation(thisform)
  53. {
  54. with (thisform)
  55. {
  56. if (emailval(email,emailtwo,"The two e-mail addresses do not match")==false) {emailtwo.focus(); email.focus(); return false;}; 
  57. if (emailvalidation(email,"Your E-mail Address Is Not Valid")==false) {email.focus(); return false;};
  58. if (emptyvalidation(name,"Please Enter Your Name")==false) {name.focus(); return false;};
  59. if (emptyvalidation(message,"Please Enter Your Equiry")==false) {message.focus(); return false;};
  60. if (emailvalidation(emailtwo,"Your E-mail Address Is Not Valid")==false) {emailtwo.focus(); return false;};
  61. /////
  62.  
  63.  
  64. /////
  65. }
  66.  
Thank again hope i can return the favour
May 11 '07 #6
gits
5,390 Expert Mod 4TB
you made an syntax-error - compare your code with the following that works :)

Expand|Select|Wrap|Line Numbers
  1. function emailvalidation(entered, alertbox) {
  2.     var x     = entered.value
  3.     var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  4.  
  5.     if (filter.test(x)) {
  6.         return true;
  7.     } else {
  8.         if (typeof alertbox != 'undefined') {
  9.             alert(alertbox);
  10.         }
  11.         return false;
  12.     }
  13. }
  14.  
May 11 '07 #7
gits
5,390 Expert Mod 4TB
... and i had a look about it to finetune the code for beautiness & readability:

Expand|Select|Wrap|Line Numbers
  1. function emailvalidation(entered, alertbox) {
  2.     var eva = entered.value;
  3.     var rex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  4.     var val = re.test(eva);
  5.  
  6.     if (!val && typeof alertbox != 'undefined') {
  7.         alert(alertbox);
  8.     }
  9.  
  10.     return val;
  11. }
  12.  
note ... thats all to do ... you don't need to check for empty value until the rex didn't match in that case ... if you want to produce different errormessages you may extend the function with a check for eva != 0 or alternativly eva != '' and differ between unmatching and emptyness perhaps you want to use:

Expand|Select|Wrap|Line Numbers
  1. // pass that to function emailvalidation
  2. alertbox = {
  3.     empty: 'message_empty',
  4.     incorr: 'message_incorrect_syntax'
  5. };
  6.  
  7. function emailvalidation(entered, alertbox) {
  8.     var eva = entered.value;
  9.     var rex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  10.     var val = re.test(eva);
  11.  
  12.     if (!val && typeof alertbox != 'undefined') {
  13.         alert(eva != 0 ? alertbox.incorr : alertbox.empty);
  14.     }
  15.  
  16.     return val;
  17. }
  18.  
this is only an idea ... if your code is working ... use that if you pefer :) but have in mind: the above example has some advantages like less lines of code, one place to maintain all validationlogic, the rex to adapt for matching-purpose ... :) good luck ...
May 11 '07 #8
Wow :) thank again, just getting to grips with the javascript syntax and i have amended emailvalidation as per your example and what you know it work brilliantly
many thank!
May 11 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Madha K | last post: by
7 posts views Thread by mhk | last post: by
5 posts views Thread by | last post: by
2 posts views Thread by daniel.boorn | last post: by
27 posts views Thread by Chris | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.