0

I have a textbox and a submit button.

I want to validate a textbox whether it is empty or not using javascript and not validator controls like RequiredFeildValidator in asp.net. i.e i want to do client side validation.

Please guide me how to do it using javascript,any tutorials will be great help.

Here are my asp controls

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

2 Answers 2

1

I would recommend a much more robust solution using one of the many fantastic JS libs that are available such as jQuery, mooTools, Dojo, yui, etc.

Since jQuery seems to be the popular kid on the block with those new to JS libs why not start here:

http://docs.jquery.com/Plugins/Validation#Example

Sign up to request clarification or add additional context in comments.

1 Comment

I definitely support this solution if anything more than simple single-page validation is needed. Even with a single page with numerous validation rules, the jQuery approach is definitely the way to go, if you don't mind getting a bit more technical. I tried to keep it pretty simple for Ihsan, as his js experience seems limited, but +1 for the suggestion. :)
0

You can have the onClick run a javascript function to check the length of the value in the text box. A link on how to set up the onclick, if you have any questions...

http://www.w3schools.com/js/js_events.asp

Also, remember that it is best practice to have scripts in the <head> tag.

<script type="text/javascript" charset="utf-8">
    function validateBox1(){
        var text = document.getElementById('TextBox1');
        if(text.value.length == 0){
            //handle validation response here
        } else {
            //submit form, or whatever the button is supposed to do...
        }
    }
</script>

4 Comments

Thank you. i am new to javascript. can u please tell me where do i add this function?
@Ishan If you add a script tag to your html/asp (preferably in the <head> tag), that will do the trick. I'll edit my answer.
Thank you again. can you tell me how to submit a page say from page1.aspx to page2.aspx. like we do Response.Redirect("page2.aspx"); if i do this on buttoon click it goes to page2.aspx without checking the validation
@Ishan It depends on how you are setting up the pages, and if you are using an MVC architecture... but if the <form> that contains these inputs is set up properly, in the else clause of this function, you can do document.(formID).submit();. Without any info as to the app architecture, that's the best I can give you... As to the "onclick", it should be fine if you change the "onclick" to this javascript function, since it is set up as a button, not submit type...

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.