473,657 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Batch form validation

Hello, all. I'm currently trying to write a script that will perform
form validation upon submission. I know that more validation should be
performed on the server's side...this is just a test to see if I can
get the client side to work.

My problem is that I'm trying to make a validator that is 100%
separated from the XHTML code. I don't even want an event handler
assignment in the form. Unfortunately, I can't see how to grab a hold
of the form and pass it down to my subroutines. The code I'm using,
which is all placed in a seperate .js file, is below:

var W3CDOM = (document.creat eElement && document.getEle mentsByTagName) ;

function init(){
if (!W3CDOM) return;
var inputform = document.getEle mentById('input form');
inputform.onsub mit = validate;
}

function validate(evt){
evt = (evt) ? evt : ((event) ? event : null);

if(evt){
var elem = (event.target) ? event.target : ((event.srcElem ent) ?
event.srcElemen t : null);

if(elem){
if(isNotEmpty(e lem.name)){
if(isName(elem. name)){
if(isNotEmpty(e lem.password)){
if(len16(elem.p assword)){
if(isNotEmpty(e lem.email)){
if(validEmail(e lem.email)){
return true;
}
}
}
}
}
}
}
}

return false;
}

function isNotEmpty(elem ){
str = elem.value;
re = /.+/;

if(!str.match(r e)){
return false;
alert(elem + " is empty!");
}

else{
return true;
}
}

function isName(elem){
str = elem.value;
re = /[a-zA-Z]*([ a-zA-Z]+\-?)*/;

if(!str.match(r e)){
return false;
alert(elem + " is not a name!");
}

else{
return true;
}
}

function len16(elem){
str = elem.value;
re = /\b.{16}\b/;

if(!str.match(r e)){
return false;
alert("Your password is not 16 characters long!");
}

else{
return true;
}
}

function validEmail(elem ){
str = elem.value;
re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

if(!str.match(r e)){
return false;
alert("You did not enter a valid e-mail address!");
}

else{
return true;
}
}

window.onload = init;

My XHTML is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test</title>
<script type="text/javascript" src="validatefo rm.js"></script>
<style type="text/css">

form p label {
font: bold 1em Arial, Helvetica, sans-serif;
float: left;
width: 30%;
}

form p {
clear: left;
margin: 0;
padding: 5px 0 0 0;
}

input.txt {
width: 200px;
}

fieldset.narrow {
width: 450px;
}

</style>

</head>

<body>

<form name="inputform " id="inputform" method="post" action="">
<fieldset class="narrow"> <legend>Pleas e input your
information</legend>
<p><label for="name">Name :</label><input id="name" type="text"
class="txt" /></p>
<p><label for="password"> Password:</label><input id="password"
type="password" class="txt" /></p>
<p><label for="email">E-mail Address:</label><input id="email"
type="text" class="txt" /></p>
<p><input type="submit" name="submit" id="submit" value="Submit"
/></p>
</fieldset>
</form>
</body>

</html>

Like I said before, it doesn't appear that my script is getting the
form as I don't even get the alert dialogue boxes to popup when I try
submitting an empty form. Any ideas on what I'm doing wrong?

Thanks. :)

Sep 1 '06 #1
5 1918

kevinmaj...@gma il.com wrote:
function validate(evt){
evt = (evt) ? evt : ((event) ? event : null);

if(evt){
var elem = (event.target) ? event.target : ((event.srcElem ent) ?
event.srcElemen t : null);

if(elem){
if(isNotEmpty(e lem.name)){
if(isName(elem. name)){
if(isNotEmpty(e lem.password)){
if(len16(elem.p assword)){
if(isNotEmpty(e lem.email)){
if(validEmail(e lem.email)){
return true;
}
}
}
}
}
}
}
}

return false;
}
[snip]

Your main problem I would surmise is with the validate function. When
you submit your form, although you have assigned an event handler to it
via javascript, there is no 'evt' being passed to your function. Nor
will it ever. Since you don't want to actually hard code the event
handler on your form, then you can do the following instead with your
function:

function validate()
{
var myForm = document.forms["inputform"];

if(myForm)
{
if(isNotEmpty(m yForm.elements["name"]))
{
//etc. etc.
}
}
}

You can also get rid of all your id's in your form and just assign the
name attribute.

Sep 1 '06 #2

web.dev wrote:
kevinmaj...@gma il.com wrote:
function validate(evt){
evt = (evt) ? evt : ((event) ? event : null);

if(evt){
var elem = (event.target) ? event.target : ((event.srcElem ent) ?
event.srcElemen t : null);

if(elem){
if(isNotEmpty(e lem.name)){
if(isName(elem. name)){
if(isNotEmpty(e lem.password)){
if(len16(elem.p assword)){
if(isNotEmpty(e lem.email)){
if(validEmail(e lem.email)){
return true;
}
}
}
}
}
}
}
}

return false;
}

[snip]

Your main problem I would surmise is with the validate function. When
you submit your form, although you have assigned an event handler to it
via javascript, there is no 'evt' being passed to your function. Nor
will it ever. Since you don't want to actually hard code the event
handler on your form, then you can do the following instead with your
function:

function validate()
{
var myForm = document.forms["inputform"];

if(myForm)
{
if(isNotEmpty(m yForm.elements["name"]))
{
//etc. etc.
}
}
}

You can also get rid of all your id's in your form and just assign the
name attribute.
Sep 1 '06 #3
web.dev wrote:
kevinmaj...@gma il.com wrote:
function validate(evt){
evt = (evt) ? evt : ((event) ? event : null);

if(evt){
var elem = (event.target) ? event.target : ((event.srcElem ent) ?
event.srcElemen t : null);

if(elem){
if(isNotEmpty(e lem.name)){
if(isName(elem. name)){
if(isNotEmpty(e lem.password)){
if(len16(elem.p assword)){
if(isNotEmpty(e lem.email)){
if(validEmail(e lem.email)){
return true;
}
}
}
}
}
}
}
}

return false;
}

[snip]

Your main problem I would surmise is with the validate function. When
you submit your form, although you have assigned an event handler to it
via javascript, there is no 'evt' being passed to your function. Nor
will it ever. Since you don't want to actually hard code the event
handler on your form, then you can do the following instead with your
function:

function validate()
{
var myForm = document.forms["inputform"];

if(myForm)
{
if(isNotEmpty(m yForm.elements["name"]))
{
//etc. etc.
}
}
}

You can also get rid of all your id's in your form and just assign the
name attribute.
Unfortunately, that doesn't seem to be working either. My updated code
(with DOM Level 0 syntax for the form elements):

function init(){
if (!W3CDOM) return;
var inputform = document.getEle mentByName('inp utform');
inputform.onsub mit = validate;
}

function validate(){
var form = document.getEle mentByName('inp utform');
if(form){
if(isNotEmpty(f orm.name)){
if(isName(form. name)){
if(isNotEmpty(f orm.password)){
if(len16(form.p assword)){
if(isNotEmpty(f orm.email)){
if(validEmail(f orm.email)){
return true;
}
}
}
}
}
}
}

return false;
}

I'm curious, why wouldn't an event be passed through the handler to the
function its assigned (inputform.onsu bmit = validate;)? And, more to
the point, why wouldn't the contents of the inputform variable be
passed to the function in that assignment as well? I'm kinda lost....

Sep 1 '06 #4
Well, judging by what some debugging alert dialogue boxes are telling
me, I'm finally getting a handle on the form after the submit button is
clicked. Now, for some reason, it appears as though its inputs aren't
being passed to the subroutines correctly. I'm currently using the
following code (debugging alerts removed and I changed elem.name to
elem.username to avoid any naming conflicts -- my XHTML was adjusted
accordingly):

/* code start */
var W3CDOM = (document.creat eElement && document.getEle mentsByTagName) ;

function init(){
if (!W3CDOM) return;
var inputform = document.getEle mentById('input form');
inputform.onsub mit = validate;
}

function validate(evt){
evt = (evt) ? evt : ((event) ? event : null);

if(evt){
var elem = (evt.target) ? evt.target : ((evt.srcElemen t) ?
evt.srcElement : null);

if(elem){
if(isNotEmpty(e lem.username)){
if(isName(elem. username)){
if(isNotEmpty(e lem.password)){
if(len16(elem.p assword)){
if(isNotEmpty(e lem.email)){
if(validEmail(e lem.email)){
return true;
}
}
}
}
}
}
}
}

else {
return false;
}
}

function isNotEmpty(elem ){
str = elem.value;
re = /.+/;

if(!str.match(r e)){
return false;
alert(elem.id + " is empty!");
}

else{
return true;
}
}

function isName(elem){
str = elem.value;
re = /[a-zA-Z]*([ a-zA-Z]+\-?)*/;

if(!str.match(r e)){
return false;
alert(elem.valu e + " is not a name!");
}

else{
return true;
}
}

function len16(elem){
str = elem.value;
re = /\b.{16}\b/;

if(!str.match(r e)){
return false;
alert("Your password is not 16 characters long!");
}

else{
return true;
}
}

function validEmail(elem ){
str = elem.value;
re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

if(!str.match(r e)){
return false;
alert("You did not enter a valid e-mail address!");
}

else{
return true;
}
}

window.onload = init;
/* code end */

In the validate function, the elem variable IS getting the form id upon
form submission. I've also been able to verify that it can see its
inputs, so something like alert(elem.user name.id); will correctly
output the id of that input element.

So, since the elem variable is being assigned correctly after the event
is captured, why aren't the subroutines working at all? I don't even
get the alert dialogue boxes for failed validation to appear when I try
submitting an empty form.

Sep 1 '06 #5
JRS: In article <11************ **********@m73g 2000cwd.googleg roups.com>
, dated Fri, 1 Sep 2006 15:09:38 remote, seen in
news:comp.lang. javascript, ke*********@gma il.com posted :
if(elem){
if(isNotEmpty(e lem.username)){
if(isName(elem. username)){
if(isNotEmpty(e lem.password)){
if(len16(elem.p assword)){
if(isNotEmpty(e lem.email)){
if(validEmail(e lem.email)){
return true;
}
}
}
}
}
}
}
}

else {
return false;
}
}
More readable to use a series of if (!OK) return false followed by
return true.
>function isNotEmpty(elem ){
str = elem.value;
re = /.+/;

if(!str.match(r e)){
return false;
alert(elem.id + " is empty!");
The alert will never be executed; put it before return.
}

else{
return true;
}
else is not needed after return. Return is not like Result.
>}
>function isName(elem){
str = elem.value;
re = /[a-zA-Z]*([ a-zA-Z]+\-?)*/;
Use the i flag.
Some faults are repeated later.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9961
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when performing the validation check on the values that were entered into the form, I simply repost the form to perform the PHP validation. If any of the values that have been entered into the form are incorrect, I display a warning message on the screen...
16
2224
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate individual fields, such as an "Email Validation Script" or a "Phone Validation Script". Is it ok to put all these scripts on page as they are or should they be joined in some way together to be one script? I'm a total JavaScript newbie and am completely...
3
1594
by: Christian Kratzer | last post by:
Hi, I have a stored procedure run periodically that assign accounting records to their respective customers based on username and other criteria. It also does all kinds of validation work on the accounting records. I would have liked to have the procedure log start and stop times using RAISE NOTICE and also store start and stop time of each run to a logging table.
3
4458
by: emman_54 | last post by:
Hi every one, I am trying to run a batch file using my asp.net application. I am using the Process class to run the batch file. When I run my web application, In the task manager, i could see cmd.exe with ASPNET as a user. But nothing happens. It can't execute the batch file. This is the code i am using to run the batch file:
9
4167
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
27
4728
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
9
2169
by: sesling | last post by:
We have several batch programs that users need to run each day to move data around our system. The batch files require the user to enter criteria when launching the program. To help simplify this I have created a form that contains several list boxes and a button to launch the batch file. This way the user does not need to type the information in. They will be able to select it from a list box. Is there a way to get the information...
3
2606
rizwan6feb
by: rizwan6feb | last post by:
Hi experts! Recently i was working on "Form Validation Using Ajax". My form validation was creating problem, when a user changes focus too quickly. I had a post related to this, but was unable to solve the problem. Here is the previous post http://bytes.com/forum/thread798289.html Trying to trace the problem I have written a code (Separate from The Form Validation) which sends 300 requests with an interval of 10 miliseconds and displays the...
1
2681
by: =?Utf-8?B?S3Jpc2huYWthbnRo?= | last post by:
I have got a requirement as follows. I am having a table RCBL_ERROR in a database in DB2. I need to create a batch file, which has to extract the rows from this table and write those extracted records to a text file. And that text file needs to be placed in the folders ORD and BKUP. The text file name should be "error_report_09302008.txt" The error report name will be like error_report_<date>.
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.