Ishan
Ishan

Reputation: 4028

Using javascript to validate content in textbox on click of a button

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" />

Upvotes: 0

Views: 8700

Answers (2)

Tim Wickstrom
Tim Wickstrom

Reputation: 5701

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

Upvotes: 1

king14nyr
king14nyr

Reputation: 715

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>

Upvotes: 0

Related Questions