Form Validation Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zylle
    New Member
    • Dec 2007
    • 2

    Form Validation Problem

    Hello,
    I am fairly new to coding and have come across something I cannot figure out. My page has a text box for a zip code then a drop down list that will take the user to a specific application page. I got the code working correctly for the form redirect and to pass the zip code that is entered in the text box. My problem is this: If the user does not enter a zip code the current text box value "Enter Zip Code" is passed along, which our application won't accept so the page results in a Site Error.
    I need to know how to replace the current field value with nothing if the user does not enter a zip code. We do not want to force them to enter a zip. The pages are ASPX.

    My code looks like this so far:

    Code:
    <script runat="server">
      string zip_code;
    
      public void Page_Load(Object sender, EventArgs e)
      {
    		string script = "function redirectMenu(){var zip=document.getElementById('zip_code'); var location=document.redirect.menu.options[document.redirect.menu.selectedIndex].value; if (zip.value != null) location=location+'&ZipCode='+zip.value; window.location.href=location;}";
    		Page.ClientScript.RegisterClientScriptBlock( this.GetType(), "redirectMenu", script, true );
      }
    </script>

    with the text field set up this way:
    Code:
    <input id="zip_code" type="text" tabindex="1" size="18" onfocus="javascript:this.value=''; this.maxLength='5';" value="Enter Zip Code" class="formtext" />
    Thank You so much for your help
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    give the input-node an id and use the forms onsubmit handler to call a function like this:

    [CODE=javascript]function chk_submit() {
    var node = document.getEle mentById('your_ input_id');

    if (/^Enter/.test(node.valu e)) {
    node.value = '';
    }
    }
    [/CODE]
    kind regards

    Comment

    • zylle
      New Member
      • Dec 2007
      • 2

      #3
      Hello,
      Thanks for the suggestion. I actually ended up working out a script on my own. I will post it here incase anyone else is facing this same issue:

      Code:
      <script language=JavaScript>
      <!-- 
      function clear_textbox()
      {
      if (document.get_quotes.zip_code.value == " Enter Zip Code ")
      document.get_quotes.zip_code.value = "";
      } 
      -->
      </script>
      Thanks again for the help, I have learned so much from these forums, Keep up the great work!

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        glad to hear you got it working ...

        a note:

        the language attribute is deprecated you should use:

        [HTML]<script type="text/javascript">[/HTML]
        and leave the comments out

        Code:
        <!-- -->
        there is no need for them.

        kind regards

        Comment

        Working...