Form Validation

Results 1 to 4 of 4

Thread: Form Validation

  1. #1
    Nathan Masek Guest

    Default Form Validation

    I am building this form for a company and I am running into a big problem. First they want validation. I say "No problem" with a grin **Thinking I can just swipe some javascript code from somewhere** <BR>THEN they stump me with this ....<BR>There will be required feilds on the form. If the user submits the form with a required field blank they want it to give them a message or something but also change the color of the text next to the field to red.<BR><BR>I HAVE figured out to do this with ASP (using A LOT of Session variables and If/then statements). But this code is way to long and the guy I am in contact with doesnt like all the Session variables. I also thought of trying to do this with javascript but can you change the color of text without refreshing the page and clearing out all the data they have entered so far?<BR><BR>So I guess my question isn&#039;t CAN it be done ... because it can ... but rather, is there a effiecient way of doing it ... with ASP or JAVASCRIPT?<BR><BR>Any help would be GREATLY appreciated.<BR><BR>- Thanks<BR>- Nathan Masek

  2. #2
    Steve Cimino Guest

    Default RE: Form Validation

    Yes, changing text color is easy in IE... don&#039;t expect it from Netscape though (go figure).<BR><BR>Your best bet is to place some small image (one that says "WARNING" or whatever) next to each field, and hide them. That way, on validation, you just unhide the image next to the appropriate field.<BR><BR>Not much coding at all.

  3. #3
    Nathan Masek Guest

    Default And this would be with Javascript? <EOP>


  4. #4
    Join Date
    Dec 1969

    Default RE: Form Validation

    Nathan, I have a generic validation function which checks for required fields on the page. Then you can change the colors by assigning a couple CLASS=thingies (Style sheet stuff) and changing the class name for the fields not filled in. Have that code to. Aw hell, here&#039;s the verify function...<BR><BR>function isblank(s) {<BR> for (var i = 0; i &#060; s.length; i++) {<BR> var c = s.charAt(i);<BR> if ((c != &#039; &#039;) && (c != &#039;
    &#039;) && (c != &#039; &#039;)) <BR> return false;<BR> }<BR> return true;<BR>}<BR> <BR>function verify(f) {<BR> var msg;<BR> var empty_fields = "";<BR> var errors = "";<BR> <BR> for (var i = 0; i &#060; f.length; i++) {<BR> var e = f.elements[i];<BR> <BR> if (null != f.elements[i].getAttribute("required")) {<BR> <BR> if ((e.type == "text") &#124&#124 (e.type == "textarea") &#124&#124 (e.type == "password")) {<BR> if ((e.value == null) &#124&#124 (e.value == "") &#124&#124 isblank(e.value)) {<BR> empty_fields += "
    " +;<BR> continue;<BR> }<BR> }<BR> <BR><BR> }<BR> <BR> if (!empty_fields && !errors) {<BR> return true;<BR> }<BR> <BR> msg = "_________________________________________________ ___

    ";<BR> msg += "The form was not submitted because of the following error(s).
    ";<BR> msg += "Please correct the errors and resubmit.
    ";<BR> msg += "_________________________________________________ ___

    ";<BR> <BR> if (empty_fields) {<BR> msg += &#039;- The following required field(s) are empty:&#039; + empty_fields + "
    ";<BR> if (errors) {<BR> msg += "
    ";<BR> }<BR> }<BR> msg += errors;<BR> alert(msg);<BR> return false;<BR>}<BR><BR>In the input fields, add a "REQUIRED" attribute... and use something like window.event.srcElement.className="newClassName"; to toggle class names and change the colors.<BR><BR>May be a bit sloppy, but most of it is copied from books and it works for me...

Posting Permissions

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