473,595 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascript form validation

4 New Member
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. {
  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. }
  14. function emptyvalidation(entered, alertbox)
  15. {
  17. with (entered)
  18. {
  19. if (value==null || value=="")
  20. {if (alertbox!="") {alert(alertbox);} return false;}
  21. else {return true;}
  22. }
  24. function emailval(thisform)
  25. {
  27. if (entered.value != second.value)
  28. {if (alertbox) {alert(alertbox);} return false;}
  29. else {return true;}
  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;};
  47. }
  48. }
May 10 '07 #1
8 1833
1,208 Recognized Expert Top Contributor
if (emailaddy == otheremailaddy) { alert("Congrats they match!"); return true; } else { return false; }
May 10 '07 #2
5,390 Recognized Expert Moderator Expert

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'; 
  3. // the following regEx returns:
  4. // ["name.surname@host.domain", "name", ".", "surname", "@", "host", ".",
  5. // "domain"] into var check
  7. var check = foo.match(/(\w+)([.])(\w+)([@])(\w+)([.])(\w+)/);
  9. // now you may do something with check ;)
hope that helps ...
May 10 '07 #3
1,208 Recognized Expert Top Contributor
he already has it validating the email address for syntax.
May 10 '07 #4
5,390 Recognized Expert Moderator Expert
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
4 New Member
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. }
  9. final solution
  11. function emailvalidation(entered, alertbox)
  12. {
  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. }
  31. ///////////////////////
  32. function emptyvalidation(entered, alertbox)
  33. {
  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. {
  45. if (entered.value != enteredtwo.value)
  46. {if (alertbox) {alert(alertbox);} return false;}
  47. else {return true;}
  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. /////
  64. /////
  65. }
Thank again hope i can return the favour
May 11 '07 #6
5,390 Recognized Expert Moderator Expert
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})+$/;
  5.     if (filter.test(x)) {
  6.         return true;
  7.     } else {
  8.         if (typeof alertbox != 'undefined') {
  9.             alert(alertbox);
  10.         }
  11.         return false;
  12.     }
  13. }
May 11 '07 #7
5,390 Recognized Expert Moderator Expert
... 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);
  6.     if (!val && typeof alertbox != 'undefined') {
  7.         alert(alertbox);
  8.     }
  10.     return val;
  11. }
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. };
  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);
  12.     if (!val && typeof alertbox != 'undefined') {
  13.         alert(eva != 0 ? alertbox.incorr : alertbox.empty);
  14.     }
  16.     return val;
  17. }
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
4 New Member
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

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 function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
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 representation can be saved as a string and recreated. (The application also has a crude workflow aspect - so XMHTML forms can be created and assigned a workflow. Forget I said anything about
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 non-utf8 characters? 2) Using javascript how to find the lengh of a string having unicode characters? e.g: For a field Name on the form, there is a corrosponding field Name varchar2(10) in DB. Through my application when i try enter 10 normal...
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.
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 roundtrip to the server. The codebehind seems to be unaware of select box members populated via javascript. So, I'm having to create my own state management solution, (i.e. rewriting the VIEWSTATE mechanism) to persist the state of these select...
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 other input fields available to fill in. I use the JavaScript OnChange function (=clientside) that creates a querystring and does a refresh of the page. It works fine.
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 in 1.1 since I could edit the .js file. Now in 2.0 I can no longer do this. Also, my code would have to be called after all client-side validation was done and was successful. Any ideas? TIA!
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! For example: A normal HTML form with out validation: <form action="foobar.htm" method="get"> <div>Name: <input type="text" name="customerName" value=""></div>
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 appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
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 zip file referenced on http://www.javascript-coder.com/html-form/javascript-form-validation.phtml I managed to get everything working and was testing through Firefox that alerts were generated. However at the very end I thought I'd better check...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.