Click here to Skip to main content
15,879,474 members
Articles / Desktop Programming / Windows Forms

Validate user input in Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.71/5 (50 votes)
26 Apr 20066 min read 427.1K   17.1K   129  
You needn't write any code for control validation; just set a control's validation information at design-time and reduce maintenance workload.
///////////////////////////////////////////////////////////
// File:			Validation.js
// File Start Date:	July 30, 2002
// Description:		Routes validation to appropriate validation handler
//
// This file must be placed in to /inetpub/wwwroot/_vti_script

String.prototype.trim = function()
{
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

String.prototype.isDate = function()
{
	var re=/^((0?\d)|(1[0-2]))(\\|\/|-)((0?\d)|([1-2]\d)|(3[0-1]))(\\|\/|-)(\d{2}|\d{4})$/;
	return re.test(this);
}

String.prototype.isTime = function()
{
	var re=/^(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;
	return re.test(this);
}

String.prototype.isDateTime = function()
{
	var re=/^((0?\d)|(1[0-2]))(\\|\/|-)((0?\d)|([1-2]\d)|(3[0-1]))(\\|\/|-)(\d{2}|\d{4})( )(((0?[1-9]|1[0-2])(:|\.)[0-5]\d((:|\.)[0-5]\d)?( )?(([aA]|[pP])[mM]))|((0?\d|1\d|2[0-3])(:|\.)[0-5]\d((:|\.)[0-5]\d)?))$/;
	
	if (this.isDate()) return true;
	if (this.isTime()) return true;
	if (re.test(this)) return true;
	return false;
}

Array.prototype.indexOf = function(val)
{
	for (var i=0; i<this.length; i++)
		if (this[i]==val) return i;
	return -1;
}

function IsInteger(val)
{
	if (val=='') return true;
	if (isNaN(parseFloat(val))) return false;
	if (parseInt(val) < parseFloat(val)) return false;
	if (val.lastIndexOf(".")==val.length-1) return false;
	return true;
}

function IsDouble(val)
{
	if (val=='') return true;
	if (isNaN(parseFloat(val))) return false;
	if (val.lastIndexOf(".")==val.length-1) return false;
	return true;
}

if (document.getElementById) //DOM Compliant browser
	document.write("<script language=\"javascript\" src=\"/_vti_script/ValidationDOM.js\"></script>");
else if (document.all) //Non-DOM Compliant IE
	document.write("<script language=\"javascript\" src=\"/_vti_script/ValidationIE.js\"></script>");
else
{
	//Force server side validation for other browsers
	document.write("<script language=\"javascript\">");
	document.write("function ClientValidation(val){return true;}");
	document.write("</script>");
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions