Generic Server side form validation

Results 1 to 6 of 6

Thread: Generic Server side form validation

  1. #1
    Join Date
    Dec 1969

    Default Generic Server side form validation

    How to make a Form Validation subroutine generic in ASP??<BR>Description:<BR>I am using some 10 forms in my application and I want to write just one validation routine which will hold good for all the forms in different pages just by including this or pasting it on that page. Can it be possible??<BR>Thanks,<BR>Sanjay.

  2. #2
    E! Guest

    Default RE: Generic Server side form validation

    This is possible... Have your form validation in an include file... Then include it in each of your forms...

  3. #3
    Join Date
    Dec 1969
    Los Angeles, CA

    Default In short...yes you can

    What you may wnat to do is pagg the control name also to your validation function <BR><BR>function abcd(objField, FieldName)<BR>{<BR>var strField = new String(objField.value);<BR>if ((strField == null) &#124&#124 (strField.length == 0))<BR>{<BR>alert("You need to enter information for " + FieldName);<BR>objField.focus();<BR>objField.selec t();<BR>return false;<BR>}<BR>return true;<BR>}<BR><BR>This way you can call the same function for all you text boxes and still have customised messages. <BR><BR>You ofcourse will HAVE to write diffefent functions for different controls and for EVERY type fo validation you wnat to do

  4. #4
    MG Guest

    Default Try this

    Here is a function that I wrote to accomplish the task that you described.<BR>It is fairly simple, but should give you a good idea of how to write one of your<BR>own. Even though this seems like a lot of code for a reusable function, it is still<BR>much, much less coding than writing 10 or even 1 form checking routine. If you like this one, <BR>also wrote a function that works like this to build forms and it is a lifesaver. I will never<BR>build a form from scratch again. If you are interested, post your email and I will contact you.<BR><BR><BR>&#039let&#039s do some validation<BR>dim fldarr, lenarr, reqarr, valarr, tparr<BR><BR>&#039**all of the arrays must have the same number of entrys<BR><BR>&#039the array of field names<BR>fldarr = array("list", "owner", "description", "messagesignature", "messagesubscribe", "messageunsubscribe")<BR><BR>&#039the array of values - get them from request.form("xxx")<BR>valarr = array(list, owner, description, messagesignature, messagesubscribe, messageunsubscribe)<BR><BR>&#039required array = if entry if required then 1 otherwise 0<BR>reqarr = array(1,0,0,1,1,1)<BR><BR>&#039lenght array - what is the max length allowed for each field<BR>lenarr = array(0,25,255,500,1000,1000)<BR><BR>&#039use "t" for text or "n" for numberic. the length check is not performed if a numeric<BR>&#039if numeric a check if performed to make sure it is numeric<BR>tparr = array("n","t","t","t","t","t")<BR><BR>&#039call the function - if len(e) = 0 then there were not data entry errors<BR>e = validateform(fldarr, lenarr, reqarr, valarr, tparr)<BR><BR>if len(e) &#062; 0 then<BR> response.write e<BR>else<BR>&#039continue code<BR>end if<BR><BR><BR><BR>function validateform(fldarr, lenarr, reqarr, valarr, tparr)<BR> dim fldrows, lenrows, reqrows, tprows, valrows, e, i, p, p2, req, flen, tp<BR> p = "&#060;p align=center&#062;"<BR> p2 = "&#060;/p&#062;"<BR> e = ""<BR> <BR> fldrows = ubound(fldarr,1)<BR> lenrows = ubound(lenarr,1)<BR> reqrows = ubound(reqarr,1)<BR> valrows = ubound(valarr,1)<BR> tprows = ubound(tparr,1)<BR> <BR> if fldrows &#060;&#062; lenrows or reqrows &#060;&#062; fldrows or reqrows &#060;&#062; lenrows or valrows &#060;&#062; lenrows then<BR> e = "Number of fields are not equal to number of values. Check your arrays."<BR> end if<BR> <BR> if len(e) = 0 then<BR> <BR> For i=0 to fldrows<BR> <BR> if reqarr(i) = 1 then<BR> if len(valarr(i)) = 0 then<BR> req = req & "&nbsp;&nbsp;" & fldarr(i) & "<BR>"<BR> end if<BR> end if<BR> <BR> &#039validate the length<BR> if tparr(i) = "t" then &#039only check the length if field type is &#039text&#039<BR> if len(valarr(i)) &#062; lenarr(i) then<BR> flen = flen & "&nbsp;&nbsp;" & fldarr(i) & " cannot be more than " & lenarr(i) & " characters.<BR>"<BR> end if<BR> end if<BR> <BR> &#039if field must be a numeric value then check it.<BR> if tparr(i) = "n" then<BR> if isnumeric(valarr(i)) = false then<BR> tp = tp & "&nbsp;&nbsp;" & fldarr(i) & "<BR>"<BR> end if<BR> end if<BR> <BR> &#039since a text field can contain any valid characters, then no checking is required<BR> next<BR> <BR> if len(req) &#060;&#062; 0 then<BR> req = "&#060;p ID=perror&#062;&#060;b&#062;These fields are required:&#060;/b&#062;<BR>" & req & "&#060;/p&#062;"<BR> end if<BR> if len(flen) &#060;&#062; 0 then<BR> flen = "&#060;p ID=perror&#062;&#060;b&#062;These fields are too long:<BR>&#060;/b&#062;" & flen & "&#060;/p&#062;"<BR> end if<BR> if len(tp) &#060;&#062; 0 then<BR> tp = "&#060;p ID=perror&#062;&#060;b&#062;These fields can only be numeric:&#060;/b&#062;<BR>" & tp & "&#060;/p&#062;"<BR> end if<BR> else<BR> <BR> end if<BR> <BR> validateform = req & flen & tp<BR>end function

  5. #5
    Akhiesh Guest

    Default Oops...sorry

    i gave you client side validations..,

  6. #6
    Join Date
    Dec 1969

    Default RE: Try this

    Hi,<BR>My email id is<BR><BR>Thanks for the help. Hoping to be in touch with you.<BR><BR>regards,<BR>Sanjay.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts