CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Posts
    47

    Javascript code for input validation & postback control

    asp.net v2.0
    I have one asp.net page, which contains 4 text boxes(asp.net controls),
    and one button control.

    I wrote validation logic for the text boxes in javascript. I want to execute
    this validation logic when I press the button, execute one by one.
    How can I achieve this

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript code for input validation & postback control

    Can you post the code with which you are working? It will give us better insight on what to suggest for you.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jan 2008
    Posts
    47

    Re: Javascript code for input validation & postback control

    Quote Originally Posted by PeejAvery
    Can you post the code with which you are working? It will give us better insight on what to suggest for you.

    I know using validation controls, but want to try javascript

    Code:
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="HtmlEtc._Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    <script type="text/javascript" >
    function validAge1(age)
    {
      if (isNaN(age) || age<10 || age>20)
     {
         alert("The age must be a number between 10 and 20");
         document.getElementById('TextBox1').focus(); 
         return false;
     }
    }
    function validAge2(age)
    {
     if (isNaN(age) || age<20 || age>30)
     {
         alert("The age must be a number between 20 and 30");
         document.getElementById('TextBox2').focus(); 
         return false;
     }
    }
    function validAge3(age)
    {
     if (isNaN(age) || age<30 || age>40)
     {
         alert("The age must be a number between 30 and 40");
         document.getElementById('TextBox3').focus(); 
         return false;
     }
    }
    function validAge4(age)
    {
     if (isNaN(age) || age<40 || age>50)
     {
         alert("The age must be a number between 40 and 50");
         document.getElementById('TextBox4').focus(); 
         return false;
     }
    }
    </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>    
            <asp:TextBox ID="TextBox1" runat="server" onblur="validAge1(this.value)"></asp:TextBox>    
            <asp:TextBox ID="TextBox2" runat="server" onblur="validAge2(this.value)"></asp:TextBox>    
            <asp:TextBox ID="TextBox3" runat="server" onblur="validAge3this.value)"></asp:TextBox>    
            <asp:TextBox ID="TextBox4" runat="server" onblur="validAge4this.value)"></asp:TextBox>    
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>
    Last edited by PeejAvery; October 16th, 2008 at 06:44 AM. Reason: Added code tags.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript code for input validation & postback control

    Well, there are a couple of kinks here.

    • Always remember that client-side validation can be easily passed by disabling JavaScript. So true validation should come from the server anyway!
    • If you have your form set to runat the server, then you can't use client-side validation.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured