Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / Javascript
Article

Client side validation without any extra code

Rate me:
Please Sign up or sign in to vote.
1.86/5 (4 votes)
4 Jun 20061 min read 38.3K   16   10
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.

<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P>function ValidateForm()<BR>{<BR>     els=new Array();<BR>     els=document.forms[1].elements;<BR>     for(i=0; i<els.length; i++)<BR>     {<BR>  <BR>         var regex="regex_" + els[i].name;<BR>         if(els[regex]!=null)<BR>         {  <BR>                var regexValue=els[regex].value;<BR>                var elValue=els[i].value;<BR>                var re = new RegExp(regexValue);<BR>                if(!elValue.match(re))<BR>                {<BR>                       var caption="caption_" + els[i].name;<BR>                       var captionValue="Field"; <BR>                       if(els[caption]!=null)<BR>                             captionValue=els[caption].value;<BR>                       alert("Invalid " + captionValue);<BR>                       return; <BR>                }<BR>         }    <BR>    <BR>  <BR>   <BR>     }<BR>     document.forms[1].submit();</P>
<P>} </P></BLOCKQUOTE>
<P dir=ltr>and your html would be like this.</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P dir=ltr><input type="text" name="txtSurname"><BR>                <input type="hidden" name= "caption_txtSurname" value="Surname"><BR>                <input type="hidden" name= "regex_txtSurname" value="^[a-zA-Z]+[a-zA-Z ]*$"></P>
<P dir=ltr><input type="text" name="txtPostCode"><BR>                              <input type="hidden" name="regex_txtPostCode" value="^([0-9]+[0-9-]*[0-9]+)?$"><BR>                <input type="hidden" name= "caption_txtPostCode" value="Post Code"></P>
<P dir=ltr><input  type="button" name="Submit" value="Submit" onClick="javascript:ValidateForm()"></P></BLOCKQUOTE>

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


Written By
Software Developer (Senior) Avanza Solutions Pakistan Karachi
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHidden fields? Pin
Richard Schaefer22-Jun-06 8:03
Richard Schaefer22-Jun-06 8:03 
AnswerRe: Hidden fields? Pin
Blumen7-May-07 18:39
Blumen7-May-07 18:39 
Generalusing one regex for a serial field Pin
zleeway18-Jun-06 16:49
zleeway18-Jun-06 16:49 
NewsJust a warning though, don't merely trust client -side for validation. [modified] Pin
volkan.ozcelik12-Jun-06 21:10
volkan.ozcelik12-Jun-06 21:10 
Generala GOOD idea Pin
CooperWu12-Jun-06 16:23
CooperWu12-Jun-06 16:23 
Generalupdate [modified] Pin
dadgad12-Jun-06 5:54
dadgad12-Jun-06 5:54 
GeneralRe: update Pin
Spiff Dog12-Jun-06 19:18
Spiff Dog12-Jun-06 19:18 
GeneralRe: update Pin
dadgad13-Jun-06 2:51
dadgad13-Jun-06 2:51 
GeneralRe: update Pin
Blumen7-May-07 19:02
Blumen7-May-07 19:02 
Why dont we simply use RegularExpressionValidator control which is part of .NET controls instead of all these?

Regards,
Blumen
GeneralRe: update Pin
Spiff Dog29-Oct-07 13:36
Spiff Dog29-Oct-07 13:36 

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

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