vote up 0 vote down star
 <script type="text/javascript">
    var msg;
    function req1(form)
    {

    if (form.textbox1.value == "" || form.textbox2.value == "")
    {   
       msg= "Please Enter Username And Password";
       return false; 
    }
    else
    {
         return true;
    }

}

    </script>

This is my javascript and i want to use it in asp.net form..so hw can i saw error msg in defalt.aspx page...or hw can i use return value(like true or false)in asp.net

flag

33% accept rate

5 Answers

vote up 3 vote down

You could use a requiredfieldvalidator for both textbox1 and textbox2 or if you need to validate them at the same time you can use a customvalidator and hookup your clientside javascript like done here: http://www.geekzilla.co.uk/ViewD27B15B4-71A4-4258-81EE-9445FAA9F634.htm

if you need to hard reference your serverside textboxes make sure you use <%= textbox1.ClientID %> in your javascript.

link|flag
vote up 3 vote down

ASP.NET provides the CustomValidator control with which you can validate multiple controls on the client and serverside with custom validation logic.

Take a look at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx for a small example.

link|flag
vote up 1 vote down

thnkx i gt solution frm another user...

function vali() { if (document.getElementById("").value == "") { var m = document.getElementById(""); m.innerHTML= "Please Enter the Name First"; return false; } else m = document.getElementById(""); m.innerHTML=""; return true; }

<asp:Label ID="L1" runat="server" Text="Name"/>
<br />
<br />
<asp:TextBox ID="T1" runat="server" />
<br/>
<br/>


<asp:Button ID="B1" runat="server" OnClientClick="return vali()" />
<br/>



</div>
</form>
link|flag
vote up 0 vote down

in the submit button add this attribute

onclientclick="req1(document.form[0])"

This will call the clientside javascript before submitting to the server.

link|flag
vote up 0 vote down

you can try this..

<script type="text/javascript">
    var msg;
    function req1(form) {
        if (document.getElementById('<%= textbox1.ClientID %>').value == "" || document.getElementById('<%= textbox1.ClientID %>').value == "") {
            msg = "Please Enter Username And Password";
            alert(msg);
            return;
        }

    }    </script>
link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.