Click here to Skip to main content
11,797,355 members (82,254 online)
Click here to Skip to main content

Client side validation without any extra code

, 4 Jun 2006 31.3K 16
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-->


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. 


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()">


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
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHidden fields? Pin
Richard Schaefer22-Jun-06 8:03
memberRichard Schaefer22-Jun-06 8:03 
AnswerRe: Hidden fields? Pin
blumenhause7-May-07 18:39
memberblumenhause7-May-07 18:39 
Generalusing one regex for a serial field Pin
zleeway18-Jun-06 16:49
memberzleeway18-Jun-06 16:49 
NewsJust a warning though, don't merely trust client -side for validation. [modified] Pin
volkan.ozcelik12-Jun-06 21:10
membervolkan.ozcelik12-Jun-06 21:10 
Generala GOOD idea Pin
WuJunyin12-Jun-06 16:23
memberWuJunyin12-Jun-06 16:23 
Generalupdate [modified] Pin
David Cook12-Jun-06 5:54
memberDavid Cook12-Jun-06 5:54 
GeneralRe: update Pin
Spiff Dog12-Jun-06 19:18
memberSpiff Dog12-Jun-06 19:18 
GeneralRe: update Pin
David Cook13-Jun-06 2:51
memberDavid Cook13-Jun-06 2:51 
GeneralRe: update Pin
blumenhause7-May-07 19:02
memberblumenhause7-May-07 19:02 
GeneralRe: update Pin
Spiff Dog29-Oct-07 13:36
memberSpiff Dog29-Oct-07 13: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
Web03 | 2.8.151002.1 | Last Updated 5 Jun 2006
Article Copyright 2006 by Sheikh Abdul Wahid
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid