Click here to Skip to main content
Licence 
First Posted 4 Jun 2006
Views 26,541
Bookmarked 16 times

Client side validation without any extra code

By | 4 Jun 2006 | Article
We can validate web forms using regular expression,just put regular expression in the value attribute of hidden field and make your life easier

Introduction

Its a simple javascript code.By using this code we can validate web forms using regular expression. The good thing of it, you dont need to write extra code, just put appropriate regular expression in the value attribute of hidden field and make your life easier. 

Detail

Idea behind that is, For validating a control of name "text1" u have to add two hidden input fields.One for validation and other for control caption. the name of validation field would be "regex_text1" and the name of caption would be "caption_text1".

Your input control would be validated according to  the regular expression which you will write in the value attribute of  "regex_text1" input field.

Another thing u have to do is, Call following function on click of a button which you are using for submission.

function ValidateForm()
{
     els=new Array();
     els=document.forms[1].elements;
     for(i=0; i<els.length; i++)
     {
  
         var regex="regex_" + els[i].name;
         if(els[regex]!=null)
         {  
                var regexValue=els[regex].value;
                var elValue=els[i].value;
                var re = new RegExp(regexValue);
                if(!elValue.match(re))
                {
                       var caption="caption_" + els[i].name;
                       var captionValue="Field"; 
                       if(els[caption]!=null)
                             captionValue=els[caption].value;
                       alert("Invalid " + captionValue);
                       return;
                }
         }    
    
  
   
     }
     document.forms[1].submit();

and your html would be like this.

<input type="text" name="txtSurname">
                <input type="hidden" name= "caption_txtSurname" value="Surname">
                <input type="hidden" name= "regex_txtSurname" value="^[a-zA-Z]+[a-zA-Z ]*$">

<input type="text" name="txtPostCode">
                              <input type="hidden" name="regex_txtPostCode" value="^([0-9]+[0-9-]*[0-9]+)?$">
                <input type="hidden" name= "caption_txtPostCode" value="Post Code">

<input  type="button" name="Submit" value="Submit" onClick="javascript:ValidateForm()">

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sheikh Abdul Wahid

Software Developer (Senior)
Avanza Solutions Pakistan Karachi
Pakistan Pakistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHidden fields? PinmemberRichard Schaefer8:03 22 Jun '06  
AnswerRe: Hidden fields? Pinmemberblumenhause18:39 7 May '07  
Generalusing one regex for a serial field Pinmemberzleeway16:49 18 Jun '06  
NewsJust a warning though, don't merely trust client -side for validation. [modified] Pinmembervolkan.ozcelik21:10 12 Jun '06  
Generala GOOD idea PinmemberWuJunyin16:23 12 Jun '06  
Generalupdate [modified] PinmemberDavid Cook5:54 12 Jun '06  
GeneralRe: update PinmemberSpiff Dog19:18 12 Jun '06  
GeneralRe: update PinmemberDavid Cook2:51 13 Jun '06  
GeneralRe: update Pinmemberblumenhause19:02 7 May '07  
GeneralRe: update PinmemberSpiff Dog13:36 29 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 5 Jun 2006
Article Copyright 2006 by Sheikh Abdul Wahid
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid