C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Form Validation using JavaScript


Posted Date: 15 Dec 2005    Resource Type: Articles    Category: General
Author: vivekthangaswamyMember Level: Gold    
Rating: Points: 12



Introduction


In this article you can learn how to validate textboxes. This will help you to learn how to validate a form. By validating we mean that we can restrict the user's input, say, to enter only numbers and only characters etc. This article is specially for beginners to web site programming.

The following code sample will explain you how to validate for numbers. The following Javascript function will help you to validate a textbox for entering only numbers.



function CheckFloating(FloatValue)
{
var thisfloat = FloatValue;
for (i=0; i < thisfloat.length; i++)
{
var ch = thisfloat.substring(i,i+1);
if (ch < "0" && ch !="." && ch !="(" && ch !=")" && ch !=" " && ch !="-")
{
return false;
}
}
return true;
}




Other Validations



Here this part we are going to the html code for our sample. The following is the html code which we are going to validate in this sample. The javaScript is the best way of validation in client side.



<html>
<head>
<title>JavaScript By Vivek</title></head>
<body>
<form name="form1" runat="server">
<script>

</script>

<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
<TR>
<TD><INPUT id="Text1" name="Text1"></TD>
<TD>Number Validation</TD>
<TD></TD>
</TR>
<TR>
<TD><INPUT id="Text2" name="Text2"></TD>
<TD></TD>
<TD></TD>
</TR>
<TR>
<TD><INPUT id="Text3" name="Text3"></TD>
<TD>Email Validation</TD>
<TD></TD>
</TR>
<TR>
<TD><INPUT id="Text4" name="Text4"></TD>
<TD>WebSiteValidation</TD>
<TD></TD>
</TR>
<TR>
<TD><INPUT id="Button1" onclick="Javascript:return Validation(this);" type="button" value="Submit"
name="Button1"></TD>
<TD></TD>
<TD></TD>
</TR>
</TABLE>

</form>
</body>
</html>



The following is a Javascript function which will restrict the text box to enter only Email IDs. And the another function is to validate for the URL. Using this function you can prevent the user from entering other then website URLs.



function EmailCheck(Email)
{
var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
var regex = new RegExp(emailReg);
return regex.test(Email);
}




How to Implement:

Just copy all the Javascript code and put inside the script tags of your HTML file and run it (ie. open it in a browser).




Responses

Author: Raja    25 Jan 2006Member Level: Bronze   Points : 0
where to use the onclick="javascript:return Validation(this);"


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Validating Textboxes using Javascript  .  Validating Textboxes in Javascript  .  Validating Textboxes  .  HTML Form Validion  .  Form Validation with Javascript  .  Form Validation using Javascript  .  Form Validation  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How best to harness the benefits of discussion forums...
Previous Resource: Windows Vista –The next generation OS
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use