JavaScript validation error [Resolved]

Posted by Samy33 under JavaScript on 3/15/2010 | Views : 2110 | Status : [Member] | Replies : 5
This is my script for validation in my project....

<script language="javascript" type="text/javascript">
function validate()
{
var username=document.getElementById("Txt_UserName").value;
var password=document.getElementById("Txt_Password").value;
if(username=="")
{
alert("Enter Your UserName")
document.getElementById("Txt_UserName").focus()
return false;
}
if(password=="")
{
alert("Enter Your Password")
document.getElementById("Txt_Password").focus()
return false;
}
}

</script>

i called the script in the .aspx.cs like

Btn_login.Attributes.Add("OnClientClick", "return validate()");

is it correct or not?

can any tell this,is it correct or not




Responses

Posted by: SurajRane on: 3/15/2010 [Member] Starter

Up
0
Down

Resolved
Ok. If you are using master page then use ClientID of contol for getting contols using js

e.g.
Here is your updated code line.

function validate() {
var username = document.getElementById("<%=Txt_UserName.ClientID%>").value;
..
..
}

in aspx.cs file

Btn_login.Attributes.Add("onclick", "return validate()");





Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SurajRane on: 3/15/2010 [Member] Starter

Up
0
Down
Here is an error.

do not use OnClientClick when you are assigning an event from .cs file use "onclick".

it will solve the problem.

wrong : Btn_login.Attributes.Add("OnClientClick", "return validate()");

Right : Btn_login.Attributes.Add("onclick", "return validate()");




Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samy33 on: 3/15/2010 [Member] Starter

Up
0
Down
Btn_login.Attributes.Add("onclick", "return validate()");

i already used this coding.this also not fired on my aspx page...In my project i am using master page,my child page contains the javascript coding....where i have to write above validation coding in masterpage or my childpage which one is correct

Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi174 on: 3/15/2010 [Member] Starter

Up
0
Down
Btn_login.Attributes.Add("OnClientClick", "return validate();");
It is correct you can add this property in the tag also

Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samy33 on: 3/16/2010 [Member] Starter

Up
0
Down
the script is working now in my project,Thank very much

Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response