Click here to Skip to main content
16,022,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi! I just want to ask why my form validation for my asp textboxes is not working. It should be like when the user does not input a text in the textbox, a description in the paragraph tag will display to please input a text. But it is not working.Please help me on solving this.

What I have tried:

Here is the code for javascript:

JavaScript
function checkForm() {
    var errors = [];

    if ($("#itemBrand").value == "") {
        errors[0] = "Please input a text!";
    }

    if ($("#itemModel").value == "") {
        errors[1] = "Please input a text!";
    }
    
    if (errors.length > 0) {
        if (errors[0] != null) {
            document.getElementById("itemBrandValidate").innerHTML = errors[0];
        }

        if (errors[1] != null) {
            document.getElementById("itemModelValidate").innerHTML = errors[1];
        }
     return false;
   }
  return true;
}


And here is the aspx:

C#
<asp:TextBox ID="itemBrand" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox><br />
                        <p  id="itemBrandValidate"></p>

<asp:TextBox ID="itemModel" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox><br />
                        <p  id="itemModelValidate"></p>

<asp:Button ID="Button1" runat="server" CssClass="submitButton" Text="Save Item" OnClick="Button1_Click" OnClientClick="return checkForm()"/>
Posted
Updated 29-Aug-17 18:32pm
Comments
ZurdoDev 29-Aug-17 10:45am    
You have to debug it.

use .val()[^] instead of .value

$("#itemBrand").val()
 
Share this answer
 
No need for custom validation script. Use the built-in validation controls instead:
<asp:TextBox ID="itemBrand" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="itemBrand" Display="Dynamic" Text="Please input a text!" />
 
<asp:TextBox ID="itemModel" runat="server" BackColor="#FFFF66" BorderColor="Black" BorderWidth="1px" Height="20px" Width="300px"></asp:TextBox><br />
<asp:RequiredFieldValidator runat="server" ControlToValidate="itemModel" Display="Dynamic" Text="Please input a text!" />
 
<asp:Button ID="Button1" runat="server" CssClass="submitButton" Text="Save Item" OnClick="Button1_Click" />

This will have the added benefit that the input will be validated on the server as well as on the client. You just need to check Page.IsValid:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    if (!Page.IsValid) return;
    
    ...
}

Validating ASP.NET Server Controls[^]
Web Forms Validation[^]
[How Do I:] Use Validation Controls in ASP.NET? | Microsoft Docs[^]
 
Share this answer
 
When you check your HTML generated, you may see the ID of the control changed. This is to prevent conflicts between other controls id. ASPX have default attribute in controls "AutoID",if you want to get the ID generated for that control, you have to use
'#<%= '+ txtID.ClientID +' %>'
or the other option not the best,but you can add the attribute
ClientIDMode="Static" 
then your ID is not going to change.
An Example it's going to look like,

function checkForm() {
    var errors = [];
 
    if ($('#<%= '+ itemBrand.ClientID +'%>').value == "") {
        errors[0] = "Please input a text!";
    }
 
    ...
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900