Introduction
There are several ways to validate your form in ASP.NET to make sure you are storing valid data into the database. The most common way by using ASP.NET validation control. The Other method that is usually common is by using javascript inserted in the syntax which run when a button is being clicked. It is usually at the top of your ASP.NET markup page.
Main
For example below I inserted two type of validation.
JavaScript can be used to validate input data in HTML forms before sending off the content to a server.
Form data that typically are checked by a JavaScript could be:
Has the user left required fields empty?
Has the user entered a valid e-mail address?
Has the user entered a valid date?
Has the user entered a valid domain?
Has the user entered text in a numeric field?
E-mail Validation
The function below checks if the content has the general syntax of an email. This means that the input data must contain at least an @ sign and a dot (.). Also, the @ must not be the first character of the email address, and the last dot must at least be one character after the @ sign. It also validate the character used in the body of email:
function emailCheck (emailStr) {
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i127) {
alert("Ths username contains invalid characters.");
return false;
}
}
for (i=0; i127) {
alert("Ths domain name contains invalid characters.");
return false;
}
}
if (user.match(userPat)==null) {
alert("The username doesn't seem to be valid.");
return false;
}
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for(i=0;i<0;i++){
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
}
}
if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}
if (len<2) {
alert("This address is missing a hostname!");
return false;
}
return true;
}
Domain Validation
In this case I am showing an example of domain name validation using javascript. When user click the button to pass value from a form, click event will be redirected to the Javascript first to check whether it is a valid form of domain. Example you want to validate (without http://www/.) somedomainname.com.
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="FormPage.aspx.cs" Inherits="FormPage" Title="Form Validation Example" %>
<asp:Content id=Content runat="Server" ContentPlaceHolderID="ContentPlaceHolder">
function checkDomain(nname)
{
var arr = new Array(
'.com','.net','.org','.biz','.coop','.info','.museum','.name',
'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
'.wf','.ye','.yt','.yu','.za','.zm','.zw');
var mai = nname;
var val = true;
var dot = mai.lastIndexOf(".");
var dname = mai.substring(0,dot);
var ext = mai.substring(dot,mai.length);
//alert(ext);
if(dot>2 && dot<57)
{
for(var i=0; i 47 && hh<59) || (hh > 64 && hh<91)
|| (hh > 96 && hh<123) || hh==45 || hh==46)
{
if((j==0 || j==dname.length-1) && hh == 45)
{
alert("Domain name should not begin or
end with '-'");
return false;
}
}
else{
alert("Your domain name should not
have special characters");
return false;
}
}
}
}
else
{
alert("Your Domain name is too short/long");
return false;
}
return true;
}
As mentioned before, you can also use ASP.NET validation control to validate like the one below, but this method will required you to import a validation library/dll.
<ASP:TEXTBOX id=txtCity RUNAT="server">
<ASP:REQUIREDFIELDVALIDATOR id=CityValidator RUNAT="server" DISPLAY="Dynamic"
ENABLEVIEWSTATE="False" ENABLECLIENTSCRIPT="False" ERRORMESSAGE="please Enter City"
CONTROLTOVALIDATE="txtCity">
</ASP:REQUIREDFIELDVALIDATOR>
when textbox is not entered properly or text not valid, and the form button is clicked, the result in your page will be like the one below:

Conclusion
The validation method above contains a small part version of the ASP.NET script and Javascript from the Users Test Validation Form. You can easily incorporate this script into your own ASP.NET forms or alternatively, you can expand this script for your own purposes.