validation in asp.net using javascript
<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开发者_Python百科 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
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.
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.
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>
in the submit button add this attribute
onclientclick="req1(document.form[0])"
This will call the clientside javascript before submitting to the server.
精彩评论