XGIS
asked on
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.
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;
}
and also u have to set the form to post its self
ASKER
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;
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;
}
}
}
ASKER
I also just added - OnClientClick="javascript: CheckABN(t his.form); " as the onclick existed.
For the Incorrect ABN you have stopped it transferring the incorrect value to the textbox which is good
For the Incorrect ABN you have stopped it transferring the incorrect value to the textbox which is good
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.
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.
ASKER
Is page validate the way to go. This thing will actually be transferred to a user control once functional.
ASKER
I just got your feedback. I will check it out!
ASKER
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
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will check it out. Thankyou
ASKER
Thankyou wdfdo1986, I have coupled your comments with another post and managed to work through this problem.
Open in new window