Home > Domino Tips > Developer > JavaScript > Javascript form validation the easy way
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVASCRIPT

Javascript form validation the easy way


04.15.2002
Rating: -3.25- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Often you need to validate a form before submission.

I created a basic set of JavaScript functions that make the alert process to the user more user friendly. You see there are so many web forms that wait until the page is posted before validation. This frustrated me a lot and often returning back to the previous page all the information is lost and you need to type it again.

Well to stop your users getting upset, you can use this code to help with html form validation.

The code is simple and effective and easy to understand and edit. There are far more complex methods, but I think that this will do you well as it is easy to find, edit and debug. We all know how frustrating it can be to debug complex JavaScript functions.

Here is some sample code below:

You can also check out other code at http://www.calaman.com

You can demo it at
http://www.connectel.co.uk/recruit/recruit_register.asp
try to submit the form without entering any fields

Regards, ametcalf@caneti.com

Code


<SCRIPT LANGUAGE="Javascript">
function ValidateForm(thisForm) {
 //alert('in validation');
 var bolValidForm = true
 var strErrorMessage = "You have not filled in some required fieldsrnrn"
 var objGiveFocusTo = null;

 if (thisForm.Email.value=="" || thisForm.Email.value.indexOf("@") <=0 || 
thisForm.Email.value.indexOf(".",thisForm.Email.value.indexOf("@"))<=0) { strErrorMessage += "You must enter a valid email address.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.Email}; bolValidForm = false; } if (thisForm.UserName.value=="") { strErrorMessage += "You must enter a username.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.UserName}; bolValidForm = false; } if (thisForm.Password.value=="" || thisForm.Password.value.length < 5) { strErrorMessage += "You must choose a password of at least 5 characters.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.Password}; bolValidForm = false; } if (thisForm.CPassword.value=="") { strErrorMessage += "You must confirm your password.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.CPassword}; bolValidForm = false; } if (thisForm.CPassword.value != thisForm.Password.value) { strErrorMessage += "Your confirmed password does not match your password.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.CPassword}; bolValidForm = false; } //This could be a select validation if the select option value = "" if (thisForm.Referral.value=="") { strErrorMessage += "You must enter how you were referred to us.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.Referral}; bolValidForm = false; } //You could also use var list = thisForm.Referral; // if an option is not selected if (list.selectedIndex == 0) { strErrorMessage += "You must enter how you were referred to us.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.Referral}; bolValidForm = false; } //Check boxes if (thisForm.WantRelocation[0].checked==false &&
thisForm.WantRelocation[1].checked==false) { strErrorMessage += "You must choose a relocation type.rn"; if (objGiveFocusTo == null){objGiveFocusTo = thisForm.WantRelocation[0]}; bolValidForm = false; } if (!bolValidForm) { //alert('failed'); alert(strErrorMessage); objGiveFocusTo.focus(); } return bolValidForm; } </SCRIPT> To call the function edit the form tag to call lthe function on submit <FORM NAME="form" Action="#" METHOD="POST" onSubmit="return ValidateForm(this)"> PS. To get an image to validate on post use code below, it will then
trigger the above form onSubmit event <input type="image" name="submitdetails" border="0" src="/images/next.gif" width="57" height="21">

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
JavaScript
Trap JavaScript runtime errors in Domino Web apps
JavaScript workaround fixes Lotus Notes 8.x PostOpen event issue
Write HTML and JavaScript in Notes view rows and columns on the Web
JavaScript detects Web browser type and version in Notes/Domino 8.0.2
JavaScript creates a jump box on a Lotus Notes Web form
How to create dynamic JavaScript in Notes Domino without formulas
Trap an attachment path via the Domino file upload control field
Converting Lotus Notes views to XML documents using JavaScript
Prevent errors on iFramed pages with JavaScript
How to add keyboard functionality for Lotus Notes documents

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts