473,396 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1817
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

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

Similar topics

5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
2
by: GIMME | last post by:
Background ... I've created a web application that allows a user to create an HTML application from IE. The application itself creates an XML representation of a XHTML form. The XHTML...
4
by: Madha K | last post by:
I am developing a web application that need to support UTF-8 characters. For client side validations, can javascript be used to handle UTF-8 characters. 1) How javascript can be used to restrict...
7
by: mhk | last post by:
Hi, Is there any way to create/set Session veriable in JavaScript. Please let me know if anyone has an idea. Thanks alot.
5
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a...
1
by: IkBenHet | last post by:
Hello, Currently I am using a large input form on a website that is based on ASP and JavaScript. Depending on the values that are filled in by the user the forms does a refresh and makes...
5
by: | last post by:
Hi all, Has anyone been able to write some custom javascript on the onclick event of submit button to do certain things like disable submit button, only submit form once etc. This was a breeze...
2
by: daniel.boorn | last post by:
Form validation using JavaScript has never been as easy and simple! We have developed a free generic form validation script that can validate any form with very little JavaScript required in form!...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
5
by: cbs7 | last post by:
Hi all I'm a complete newbie to web development and Javascript especially. I've been creating a form for a webpage and have used a validation script gen_validatorv2.js which I downloaded from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.