shanish
shanish

Reputation: 1984

jQuery validation without plugins

Is it possible to implement the jQuery validation without using any plugins for this..

In my asp.net application, am using asp:textboxes to receive the inputs but the button is not an asp control, am calling the jQuery file at the button click event...I need to validate the inputs without using any plugins.....how can I achieve this....

I tried like put the requiredfield validator after textbox like,

<asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox>
<div>
   <asp:RequiredFieldValidator ID="reqValName" ControlToValidate="txtName" 
        runat="server" CssClass="validation" ErrorMessage="*Required">
   </asp:RequiredFieldValidator>
</div>

and in my js file am checking the validation like,

$(function () {
$("#txtName").change(function () {
    if ($("#txtName").val() == "") {
        $(".reqValName").val();
    }
});

its working when I try deleting the value in the textbox(making it as empty), if I leave from here it shows the "*Required" error....fine....I tried with blur function, its not working and also, I need to stop the button click event before clearing this validation errors, in server side if it is asp button, I never worried bout this, but here it is html, I dunno how to do, and also how can I use regular expression for this textbox.....

Upvotes: 0

Views: 497

Answers (1)

Ray Cheng
Ray Cheng

Reputation: 12576

if you do want server side validation, you can always turn the html button to server side control by adding runat="server" and then you can provide a button click event to validate data on server side. See below for sample:

in html:

<input runat="server" type="submit" value="Submit" 
       id="btnSubmit" onserverclick="btnSubmit_Click" />

in code behind:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if(txtName.Text == "Ray")
    {
        reqValName.IsValid = false; // Ray is taken
    }
}

Upvotes: 1

Related Questions