input checking
When requesting input from users it saves a lot of time for the user if the input is checked before the form values are posted back to the server.
Javascript is very useful for this since it operates within the browser and there fore acts immediately and does not rely on server response times.
It is worth being aware that in most browsers it is possible to disable javascript support so ensuring that javascript is enabled before the browser reaches a page that relies upon it can be a useful tactic.
demonstration
Please fill in this form as badly as you can, including missing out data, and then submitting it.
(Please note NONE of the data is captured in any way and is not retained by any system)
the script
Well there isn't one script per se, just a series of methods we employ within this framework. The one used on this page is...
description
<SCRIPT LANGUAGE="Javascript" type="text/javascript">
<!--
function integercheck (instring)
{ var n;
for (n = 0; n < instring.length; n++) {
var m = instring.charAt(n);
if (m <= "0" || m >= "9") return false;
}
return true;
}
function validateinput(form){
var title = form.title.value;
var enddate = form.date.value;
var email = form.email.value;
var age = form.age.value;
age = age/1;
var agecheck = integercheck(age);
var colour = form.colour.value;
var emailspace = email.indexOf(' ',0) + 1
var emailat = email.indexOf('@',0) + 1
var emaildot = email.indexOf('.',0) + 1
var dateslash = enddate.indexOf('\/',0) + 1
var datebits = enddate.split("\/")
var month = parseInt(datebits[0])
var monthcheck = integercheck(month)
var day = parseInt(datebits[1])
var daycheck = integercheck(day)
var year = parseInt(datebits[2])
var yearcheck = integercheck(year)
var errorsfound = false;
var errormessage = "There were errors\n====================\n\n";
if (title.length == 0){
errormessage = errormessage +"No title has been supplied.\n";
errorsfound = true;
}
if (enddate.length == 0){
errormessage = errormessage +"No date has been supplied.\n";
errorsfound = true;
}
if (age.length == 0){
errormessage = errormessage +"No age has been supplied.\n";
errorsfound = true;
}
if (colour == 0){
errormessage = errormessage +"No colour selection has been made.\n";
errorsfound = true;
}
if (email.length == 0){
errormessage = errormessage +"No contact email has been supplied.\n";
errorsfound = true;
}
if (!agecheck){
errormessage = errormessage +"The age supplied is not a proper number.\n";
errorsfound = true;
}
if (emailspace > 0){
errormessage = errormessage +"The email address contains a space.\nemail addresses should be in the format whoever@whatever.com.\n";
errorsfound = true;
}
if (emailat == 0){
errormessage = errormessage +"The email address does not contain a '@' symbol.\nemail addresses should be in the format whoever@whatever.com.\n";
errorsfound = true;
}
if (emaildot == 0){
errormessage = errormessage +"The email address does not contain any dots.\nemail addresses should be in the format whoever@whatever.com\n";
errorsfound = true;
}
if (dateslash == 0){
errormessage = errormessage +"The date is not properly formatted.\nThe date should be in the format mm\/dd\/yyyy.\n";
errorsfound = true;
}
if (year < 2001){
errormessage = errormessage +"The year value has an error.\nThe date should be in the format mm\/dd\/yyyy.\n";
errorsfound = true;
}
if (!yearcheck){
errormessage = errormessage +"The year supplied is not a proper number.\n";
errorsfound = true;
}
if (month > 12){
errormessage = errormessage +"There are only 12 month in a year!.\nThe date should be in the format mm\/dd\/yyyy.\n";
errorsfound = true;
}
if (!monthcheck){
errormessage = errormessage +"The month supplied is not a proper number.\n";
errorsfound = true;
}
if (day > 31){
errormessage = errormessage +"There are a maximum of 31 days in a month!.\nThe date should be in the format mm\/dd\/yyyy.\n";
errorsfound = true;
}
if (!daycheck){
errormessage = errormessage +"The day supplied is not a proper number.\n";
errorsfound = true;
}
if (errorsfound){
alert (errormessage)
return false
}
}
//-->
</SCRIPT>
deployment
Javscript should just be used as a convenient and quick way to help detect user errors in filling out forms.
It must not be used as the sole method of checking content before it is processed via a script.
this script is free for use...
... however we would appreciate it if you gave some acknowledgement of where you got it from.
It would also be nice if you let us know you were using it so that we could let you know of any enhancements that come to light.
contact us at: support@rylands-internet-solutions.co.uk
|