Client side validation in javascript

When we perform client side validations using javascript:


<script language="javascript>
function validate()
{
var txtCollege=document.getElementById("<%=txtCollege.ClientID%>");
if(txtCollege.value=="")
{
alert('Enter CollegeName');
txtCollege.focus();
return false;
}
}
<script/>


here is call button event:


onkeypress="return validate()"


Comments

Author: Shameer19 Nov 2009 Member Level: Gold   Points : 0

Please let me know what is the problem you face?

Author: Deepa27 Nov 2009 Member Level: Bronze   Points : 2


I have two textboxes txtFirstName,txtLastName.These are Mandatory fields...so I am using client side javascript for validaion.if textbox has no value, you will get alert msg.

function Validate()
{
if(document.getElementById('txtFirstName')=="")
strMsg="FirstName \n";
if(document.getElementById('txtLastName')=="")
strMsg="LastName \n";
if(strMsg!="")
{
alert("The following fields are mandatory\n" + strMsg);
return false;
}
else
return true;
}

You can Call this function

onClientClick="javascript:return Validate();"



Author: nirav07 Apr 2010 Member Level: Gold   Points : 0

u r perfectly right deepa

Author: Ultimaterengan18 Jun 2010 Member Level: Gold   Points : 0

HI
What error.............

Author: Maheshbabu11 Oct 2010 Member Level: Bronze   Points : 1

Author: Mayur13 Oct 2010 Member Level: Gold   Points : 1

Hi

Try this to remove error.


Author: Amit Shah14 Oct 2010 Member Level: Gold   Points : 1

Hello,

You should use above javascript function with onClientClick of button or onBlur of Textbox control.

Thanks
Amit Shah



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: