0

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.....

4
  • what have you tried?If you show us some part of your code and from where you're unable to get it worked we can help you. Commented Apr 28, 2012 at 7:44
  • no, I have no idea, when I went through the internet, I found the jQuery validations using plugins only, thats what am asking Commented Apr 28, 2012 at 7:51
  • 2
    jQuery validation is a plugin for jQuery. that being said, it's written using jQuery code. You could write your own code using regexp and onchange, submit, click, keyup/keydown/keypress, etc, a whole list of things using just jQuery. Commented Apr 28, 2012 at 8:01
  • yeah its a great idea, I'll work on it, thanks Commented Apr 28, 2012 at 8:07

1 Answer 1

1

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
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I dont understand, if I use runat="server" can I access the jQuery in the button click, as well as check validation?
Yes. Adding runat="server" gives the button ability to trigger server side code and it can still work with client side javascript. You may need to set ClientIDMode="Static" to make it client side script friendly.
I just edited my question, can u solve my query, or give some link or coding example, thanks
Since you are using asp.net required field validation control, you don't need jquery or the javascript code for onchange. What is not working for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.