• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Transfer Javascript Validation to C# in ASP.NET Validation

I am trying to prevent incorrect data being loaded to a database.

I have some javascript that checks the data entered. This returns an alert to say if it is valid or not.

I then use the button click event to transfer the "Valid" value to the textbox and allow the Submit button to be displayed.

The problem is that it transfers both valid and invalid values to the textbox.

Can you check my code and make adjustments so it works.
ASPX Form Code

<head runat="server">
    <title></title>
   <script language="JavaScript" type="text/javascript">

        Weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);

        function CheckABN(form) {

            ABN = (form.abn1.value - 10) + form.abn2.value + form.abn3.value + form.abn4.value;

            total = 0;

            for (i = 0; i < 11; i++)
                total += Weights[i] * ABN.charAt(i);

            if (total == 0 || total % 89 != 0) {
                alert("Error, the ABN " + form.abn1.value + " " + form.abn2.value + " " + form.abn3.value + " " + form.abn4.value + " is invalid !");
            }
            else
                alert("The ABN " + form.abn1.value + " " + form.abn2.value + " " + form.abn3.value + " " + form.abn4.value + " is valid !");

        }
 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
	Enter your ABN: 
<asp:TextBox ID="abn1" runat="server" MaxLength="2" Width="17"></asp:TextBox>
<asp:TextBox ID="abn2" runat="server" MaxLength="3" Width="27"></asp:TextBox>
<asp:TextBox ID="abn3" runat="server" MaxLength="3" Width="27"></asp:TextBox>
<asp:TextBox ID="abn4" runat="server" MaxLength="3" Width="27"></asp:TextBox>
<asp:Button ID="btnABNCheckValid" runat="server" Text="Validate ABN" OnClientClick="CheckABN(this.form)" onclick="Button1_Click"/>
<asp:TextBox ID="txtTest" runat="server" MaxLength="11"></asp:TextBox>
<br /><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnABNValidSubmit" runat="server" Text="Submit Valid ABN"></asp:Button>
  </center>
    </div>
    </form>
</body>


C# Code

    protected void Page_Load(object sender, EventArgs e)
    {
        btnABNValidSubmit.Visible = false;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid == true)

            txtTest.Text = string.Concat(abn1.Text, abn2.Text, abn3.Text, abn4.Text);
        Label1.Text = "Your ABN is valid and ready for submission!";
             btnABNValidSubmit.Visible = true;

        else
            txtTest.Text = null;
            Label1.Text = "Please check your ABN!";
            btnABNValidSubmit.Visible = false;

    }

Open in new window

0
XGIS
Asked:
XGIS
  • 7
  • 4
1 Solution
 
wdfdo1986Commented:
Try in this way
<head runat="server">
    <title></title>
   <script language="JavaScript" type="text/javascript">

        Weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);

        function CheckABN(form) {

            ABN = (form.abn1.value - 10) + form.abn2.value + form.abn3.value + form.abn4.value;

            total = 0;

            for (i = 0; i < 11; i++)
                total += Weights[i] * ABN.charAt(i);

            if (total == 0 || total % 89 != 0) {
                alert("Error, the ABN " + form.abn1.value + " " + form.abn2.value + " " + form.abn3.value + " " + form.abn4.value + " is invalid !");
            }
            else{
                alert("The ABN " + form.abn1.value + " " + form.abn2.value + " " + form.abn3.value + " " + form.abn4.value + " is valid !");
                this.form.submit();
            }

        }
 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <center>
	Enter your ABN: 
<asp:TextBox ID="abn1" runat="server" MaxLength="2" Width="17"></asp:TextBox>
<asp:TextBox ID="abn2" runat="server" MaxLength="3" Width="27"></asp:TextBox>
<asp:TextBox ID="abn3" runat="server" MaxLength="3" Width="27"></asp:TextBox>
<asp:TextBox ID="abn4" runat="server" MaxLength="3" Width="27"></asp:TextBox>
<asp:Button ID="btnABNCheckValid" runat="server" Text="Validate ABN" onclick="javascript:CheckABN(this.form);"/>
<asp:TextBox ID="txtTest" runat="server" MaxLength="11"></asp:TextBox>
<br /><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnABNValidSubmit" runat="server" Text="Submit Valid ABN"></asp:Button>
  </center>
    </div>
    </form>
</body>


C# Code

    protected void Page_Load(object sender, EventArgs e)
    {
        btnABNValidSubmit.Visible = false;
        if(isPostBack){
        Page.Validate();
        if (Page.IsValid == true)

            txtTest.Text = string.Concat(abn1.Text, abn2.Text, abn3.Text, abn4.Text);
        Label1.Text = "Your ABN is valid and ready for submission!";
             btnABNValidSubmit.Visible = true;

        else
            txtTest.Text = null;
            Label1.Text = "Please check your ABN!";
            btnABNValidSubmit.Visible = false;
        }

    }

Open in new window

0
 
wdfdo1986Commented:
and also u have to set the form to post its self
0
 
XGISAuthor Commented:
Hello wdfdo1986: Two things;

The else in the code behind for my example was invalid. It seems to only accept the string concat and nothing else. So i removed the;

Label1.Text = "Your ABN is valid and ready for submission!";
btnABNValidSubmit.Visible = true;

Unfortunately your javascript pulls up on "   this.form.submit();  " which might relate to the "set the form to post its self" comment you added.

The behind code no looks as follows;

using System;
using System.Web;
using System.Web.UI;

public partial class Modules_ABNNNNNNNN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnABNValidSubmit.Visible = false;

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        btnABNValidSubmit.Visible = false;
        if (!Page.IsPostBack)
        {
            Page.Validate();
            if (Page.IsValid == true)

                txtTest.Text = string.Concat(abn1.Text, abn2.Text, abn3.Text, abn4.Text);

            else
                txtTest.Text = null;
            Label1.Text = "Please check your ABN!";
            btnABNValidSubmit.Visible = false;
        }

    }
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
XGISAuthor Commented:
I also just added  - OnClientClick="javascript:CheckABN(this.form);"  as the onclick existed.

For the Incorrect ABN you have stopped it transferring the incorrect value to the textbox which is good
0
 
wdfdo1986Commented:
i'll get back to u when i have access to visual studio
right now i am using linux
how ever u can post back the page and that will make the IsPostBack true which u can check at the Page_Load and proceed.
check that form can have an event like OnSubmit and bind it to the code behind event handler.
0
 
XGISAuthor Commented:
Is page validate the way to go.  This thing will actually be transferred to a user control once functional.
0
 
XGISAuthor Commented:
I just got your feedback. I will check it out!
0
 
XGISAuthor Commented:
I have removed the onclick and button event and moved it into page load;

It still works the same and is successful for the False response but still falls over at the

 this.form.submit();

I will wait till you get access to VS

protected void Page_Load(object sender, EventArgs e)
    {
        btnABNValidSubmit.Visible = false;
        txtTest.Text = null;
        Label1.Text = "";
        if (!Page.IsPostBack)
        {
            Page.Validate();
                if (Page.IsValid == true)

                    txtTest.Text = string.Concat(abn1.Text, abn2.Text, abn3.Text, abn4.Text);

                else
                    txtTest.Text = null;
                    Label1.Text = "Please check your ABN!";
                    btnABNValidSubmit.Visible = false;
        }
    }

Open in new window

0
 
wdfdo1986Commented:
Hello,
I completely forgot this and I apologize for that.

I will comment regarding the following quote

"The else in the code behind for my example was invalid. It seems to only accept the string concat and nothing else. So i removed the;

Label1.Text = "Your ABN is valid and ready for submission!";
btnABNValidSubmit.Visible = true;

Unfortunately your javascript pulls up on "   this.form.submit();  " which might relate to the "set the form to post its self" comment you added."

first form should have an action like this

<form id="form1" action="" runat="server">

when we set action="" it will post its self.
0
 
XGISAuthor Commented:
I will check it out. Thankyou
0
 
XGISAuthor Commented:
Thankyou wdfdo1986, I have coupled your comments with another post and managed to work through this problem.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now