codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  How work this reusable form validation script ?  jhonilson at 18:43 on Thursday, May 13, 2010
 

hello
I had found this javascript code in a book but I do not know how it work, is to say, I do not how call it from my html code!
The book say that I need to call the class : <input type="text" class="email" id="email" name="email" /> o <input type="text" class="required" id="username" name="username"/>
This code is a generical form validator and I find it very helpfull!!
The full code is:
[code]
var FormValidation =
{
rules:
{
required: /./,
requiredNotWhitespace: /\S/,
positiveInteger: /^\d*[1-9]\d*$/,
positiveOrZeroInteger: /^\d+$/,
integer: /^-?\d+$/,
decimal: /^-?\d+(\.\d+)?$/,
email: /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/,
telephone: /^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/
},errors:
{
required: "Please fill in this required field.",
requiredNotWhitespace: "Please fill in this required field.",
positiveInteger:
"This field may only contain a positive whole number.",
positiveOrZeroInteger: "This field may only contain a " +
"non-negative whole number.",
integer: "This field may only contain a whole number.",
decimal: "This field may only contain a number.",
email: "Please enter a valid email address into this field.",
telephone:
"Please enter a valid telephone number into this field."
},init: function()
{
var forms = document.getElementsByTagName("form");
for (var i = 0; i < forms.length; i++)
{
Core.addEventListener(
forms, "submit", FormValidation.submitListener);
}
},submitListener: function(event)
{
var fields = this.elements;
for (var i = 0; i < fields.length; i++)
{
var className = fields.className;
var classRegExp = /(^| )(\S+)( |$)/g;
var classResult;
while (classResult = classRegExp.exec(className))
{
var oneClass = classResult[2];
var rule = FormValidation.rules[oneClass];
if (typeof rule != "undefined")
{
if (!rule.test(fields.value))
{
fields.focus();
alert(FormValidation.errors[oneClass]);
Core.preventDefault(event);
return;
}
}
}
}
}
}
[/code]
I know how to call function from html but in this code I just see labels!! as : "init", "submitListener", etc
Please can anybody tell me how it work??

Thank you and Excume me my bad english.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Argh, Javascript + Forms and me = DISASTER!
•  Need javascript code to identify non-searchable PDF
•  How work this reusable form validation script ?
•  Re: Copy files from one directory to another in a local network
•  DLL
•  calendar or event management
•  Re: how to convert wav file to text file
•  Re: huffman encoding and decoding in C++...
•  Re: How to Change user to root using ssh::expect


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010