Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need validation for an simple field like text box,radio button or dropdown. label contains a name it should validate the details entering in the textbox like that.the field should not be empty and it should contain limited characters and no numeric should be allowed.if nothing is entered or selected it should display an error message.
Posted
Comments
meeraradha 24-Feb-12 0:25am    
please tell the code for radiobutton list validation
Varun Sareen 24-Feb-12 0:48am    
add tag asp.net as well
Varun Sareen 24-Feb-12 0:49am    
how much limit for the characters?
meeraradha 24-Feb-12 0:53am    
for an numeric field the limit is 6 numbers and for character the field should not left blank.if blank error message should be display.
meeraradha 24-Feb-12 2:04am    
thank you..i got the result for that.

try this:-

Lets say you have a form like this:-
<form name="subscribe" id="subscribe_frm" action="#">
Your Name: <input type="text" name="name" id="txt_name" />
<input type="button" name="submit" value="Submit">
onclick="validateTextBox();" onleyup="validChar();" />
</input></form>

<script language=""javascript"">
function validateTextBox()
{
 var txtN=document.getElementById('txt_Name');
 if(txtN.value=='')
 {
  alert('Textbox can't be blank');
  return false;
 }
 if(txtN.length>=[type the no of charaters you want the text to be limit])
 {
  alert('[Your prompt]');
  return false;
 }
}
function validChar()
{
 var txtN=document.getElementById('txt_Name');
 if(!txtN.value.match(/^[a-zA-Z]+$/))
 {
  alert('Enter only Characters');
  return false;
 }
}
</script>


Please don't forget to mark this as your answer if it helps you out. Queries are welcome
 
Share this answer
 
v2
Javascript code :


<script language="javascript" type="text/javascript">
function validate()
{
if (document.getElementById("<%=txt_uname.ClientID%>").value=="")
{
alert("UserName Feild can not be blank");
document.getElementById("<%=txt_uname.ClientID%>").focus();
return false;
}
if (document.getElementById("<%=txt_pwd.ClientID%>").value=="")
{
alert("Password Feild can not be blank");
document.getElementById("<%=txt_pwd.ClientID%>").focus();
return false;
}
if (document.getElementById("<%=txt_cpwd.ClientID%>").value=="")
{
alert("Confirm Passowrd Feild can not be blank");
document.getElementById("<%=txt_cpwd.ClientID%>").focus();
return false;
}

if (document.getElementById("<%=txt_age.ClientID%>").value=="")
{
alert("Age Feild can not be blank");
document.getElementById("<%=txt_age.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=txt_email.ClientID %>").value=="")
{
alert("Email id can not be blank");
document.getElementById("<%=txt_email.ClientID %>").focus();
return false;
}
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("<%=txt_email.ClientID %>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txt_email.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txt_mobile.ClientID%>").value=="")
{
alert("Mobile No is not valid");
document.getElementById("<%=txt_mobile.ClientID%>").focus();
return false;
}
var digits=/^([0-9]{10})$/;
var digitsid=document.getElementById("<%=txt_mobile.ClientID %>").value;
var digitsArray = digitsid.match(digits);
var temp;
if (digitsArray == null)
{
alert("Your mobile seems incorrect. Please try again.");
document.getElementById("<%=txt_mobile.ClientID %>").focus();
return false;
}

return true;
}
</script>




For submit button :


<asp:Button ID="btn_sub" runat="server" Font-Names="Verdana" Text="Submit" onclick="btn_sub_Click"Width="77px" OnClientClick=" return validate()"/>
 
Share this answer
 
Comments
meeraradha 24-Feb-12 2:04am    
thank you.. i got the result for the query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900