Click here to Skip to main content
Click here to Skip to main content

Client side validation without any extra code

, 4 Jun 2006
Rate this:
Please Sign up or sign in to vote.
We can validate web forms using regular expression,just put regular expression in the value attribute of hidden field and make your life easier
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

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

Share

About the Author

Sheikh Abdul Wahid
Software Developer (Senior) Avanza Solutions Pakistan Karachi
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
QuestionHidden fields? PinmemberRichard Schaefer22-Jun-06 9:03 
AnswerRe: Hidden fields? Pinmemberblumenhause7-May-07 19:39 
Generalusing one regex for a serial field Pinmemberzleeway18-Jun-06 17:49 
NewsJust a warning though, don't merely trust client -side for validation. [modified] Pinmembervolkan.ozcelik12-Jun-06 22:10 
Generala GOOD idea PinmemberWuJunyin12-Jun-06 17:23 
Generalupdate [modified] PinmemberDavid Cook12-Jun-06 6:54 
GeneralRe: update PinmemberSpiff Dog12-Jun-06 20:18 
GeneralRe: update PinmemberDavid Cook13-Jun-06 3:51 
GeneralRe: update Pinmemberblumenhause7-May-07 20:02 
GeneralRe: update PinmemberSpiff Dog29-Oct-07 14:36 

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.

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