473,412 Members | 4,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

DOM form validation and troubles with FF...

Hi.
I've managed this simple snippet:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript" type="text/JavaScript">
var usernameError = 'Invalid String username';
var passwordError = 'Invalid String password';
var emailError = 'Invalid String email';
function validateForm()
{ valid=true;
if(objForm.getAttribute('name').toString()=='frmRe gister')
{
var username = checkField(objForm.elements[1],'username');
var password = checkField(objForm.elements[2],'password');
var email = checkField(objForm.elements[3],'email');
if(!isValidString(username.value))
{
showError(username,usernameError);
}
if(!isValidString(password.value))
{
showError(password,passwordError);
}
if(!isValidEmail(email.value))
{
showError(email,emailError);
}
}
return valid;
}
function checkField(element,fieldName)
{
if(element.getAttribute('name').toString()==fieldN ame){return
element;}
else{return;}
}
function showError(element,message)
{
if(!element.errorNode)
{
addEvent(element,'change',hideError, false);
var spanContent = document.createTextNode(message);
element.nextSibling.appendChild(spanContent);
element.errorNode = spanContent;
}
valid=false;
}
function hideError(e)
{
var fInput = getTarget(e);
if(text = fInput.nextSibling.firstChild)
{
fInput.nextSibling.removeChild(text);
}
fInput.onchange=null;
fInput.errorNode=null;
}
function isValidEmail(str)
{
var regExEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,2}$/;
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
return (regExEmail.test(str) && !str.match(illegalChars));
};
function isValidString(str)
{
var regExvalidString = /^([\w]+)$/;
return regExvalidString.test(str);
};
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
};
function getTarget(e)
{
var target = window.event ? window.event.srcElement : e ? e.target :
null;
if (!target){return false;}
return target;
};
function Init()
{
objForm = document.getElementsByTagName('form')[0];
objForm.elements[1].focus();
addEvent(objForm,'submit',validateForm, false);
}
addEvent(window,'load',Init, false);
</script>
<style type="text/css">
* {
margin:0px;
padding:0px
}
a:link, a:visited,a:hover,a:active {
text-decoration : none;
color:#62AC24;
font-weight:800;
}
body {
text-align:center;
background-color:#EDF6D9;
color:#28780A
}
div#container {
margin: 0 auto;
width:750px;
text-align:left;
}
div#header {
}
div#header h1 {
text-align:center;
}
div#login {
text-align:center;
}
div#footer {
}
div#footer h3 {
text-align:center
}
img {margin:5px 0px}
/* END MAIN */

/* FORM */
form {
width:750px;
margin: 0px auto;
text-align:left;
}
form fieldset {
width:750px;
display:block;
border:1px solid #77B608;
padding:5px;
font-family:verdana, sans-serif;
margin-bottom:0.5em;
line-height:1.5em;
}
form legend {
width:150px;
margin-bottom:5px;
border:2px solid #fff;
padding:3px;
background-color:#E4F8AD;
text-align:center;
font-family:georgia, sans-serif;
font-size:1.1em;
font-weight:bold;
}
form label {
float:left;
width:250px;
margin-right:5px;
line-height:20px;
text-align:right;
}
form label a#refresh {
line-height:40px;

}
form input {
width:250px;
border:1px solid #fff;
padding-left:5px
}
form input#remember {
width:15px;
}
form input#bottom {
width:250px;
margin-top:5px;
background-color:#B4CE06;
color:#FFFFFF;
font-weight:bold
}
br.frmClear {
clear:both
}
span.errors {
color:#FF0000
}
p#invalidLogin {
color:#FF0000
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>TwriTeLy</h1>
</div>
<div id="login">
<form name="frmRegister" action="ServerSideControl" method="post">
<fieldset>
<legend>Register</legend>
<label>Username : </label>
<input type="text" name="username" value="" maxlength="32" /><span
class="errors" ></span><br class="frmClear" />
<label>Password : </label>
<input type="password" name="password" value="" maxlength="32" /><span
class="errors" ></span><br class="frmClear" />
<label>Email : </label>
<input type="text" name="email" value="" maxlength="32" /><span
class="errors" ></span><br class="frmClear" />
<label>&nbsp;&nbsp;</label>
<input id="bottom" type="submit" name="register" value="Register" />
</fieldset>
</form>
</div>
<div id="footer"><h3>By Whisher</h3></div>
</div>
</body>
</html>
Well it works fine with IE but it doesn't with FF.
I know FF is standards compliance :) then is my
mistake but I'm not able to find it out :(
Do you have any suggestion ?
I hope so ;)
Take care.

Dec 17 '06 #1
9 1537
I'm awfully sorry I forgot
the trouble.
With FF the form is just submitted
with the error (valid is FALSE !)
Bye.

Dec 17 '06 #2
whisher wrote:
function validateForm()
function validateForm (evt)
{ valid=true;
if(objForm.getAttribute('name').toString()=='frmRe gister')
{
var username = checkField(objForm.elements[1],'username');
var password = checkField(objForm.elements[2],'password');
var email = checkField(objForm.elements[3],'email');
if(!isValidString(username.value))
{
showError(username,usernameError);
}
if(!isValidString(password.value))
{
showError(password,passwordError);
}
if(!isValidEmail(email.value))
{
showError(email,emailError);
}
}
if (!valid && evt && typeof evt.preventDefault != 'undefined') {
evt.preventDefault();
}
return valid;
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 17 '06 #3
Thanks so much buddy.
I arrived to the same conclusion.
An other annoying trouble why
it doesn't work if I put cancelClick(e)
instead of fInput.onchange=null
The new code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Register</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript" type="text/JavaScript">
var usernameError = 'Invalid String username';
var passwordError = 'Invalid String password';
var emailError = 'Invalid String email';
function validateForm(e)
{ valid=true;
if(objForm.getAttribute('name').toString()=='frmRe gister')
{
var username = checkField(objForm.elements[1],'username');
var password = checkField(objForm.elements[2],'password');
var email = checkField(objForm.elements[3],'email');
if(!isValidString(username.value))
{
showError(username,usernameError);
}
if(!isValidString(password.value))
{
showError(password,passwordError);
}
if(!isValidEmail(email.value))
{
showError(email,emailError);
}
if(!valid)
{
cancelClick(e);
}
}
return valid;
}
function checkField(element,fieldName)
{
if(element.getAttribute('name').toString()==fieldN ame){return
element;}
else{return;}
}
function showError(element,message)
{
if(!element.errorNode)
{
addEvent(element,'change',hideError, false);
var spanContent = document.createTextNode(message);
element.nextSibling.appendChild(spanContent);
element.errorNode = spanContent;
}
valid=false;
}
function hideError(e)
{
var fInput = getTarget(e);
if(text = fInput.nextSibling.firstChild)
{
fInput.nextSibling.removeChild(text);
}
fInput.onchange=null;
////////////
//
// WHY ON EARTH IT DOESN'T IF I USE IT INSTEAD OF fInput.onchange=null

// cancelClick(e);
//
/////////
fInput.errorNode=null;
}
function isValidEmail(str)
{
var regExEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,2}$/;
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
return (regExEmail.test(str) && !str.match(illegalChars));
};
function isValidString(str)
{
var regExvalidString = /^([\w]+)$/;
return regExvalidString.test(str);
};
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
};
function getTarget(e)
{
var target = window.event ? window.event.srcElement : e ? e.target :
null;
if (!target){return false;}
return target;
};
function cancelClick(e)
{
if (window.event)
{
window.event.cancelBubble = true;
window.event.returnValue = false;
return;
}
if (e)
{
e.stopPropagation();
e.preventDefault();
}
};
function Init()
{
objForm = document.getElementsByTagName('form')[0];
objForm.elements[1].focus();
addEvent(objForm,'submit',validateForm, false);
}
addEvent(window,'load',Init, false);
</script>
<style type="text/css">
* {
margin:0px;
padding:0px
}
a:link, a:visited,a:hover,a:active {
text-decoration : none;
color:#62AC24;
font-weight:800;
}
body {
text-align:center;
background-color:#EDF6D9;
color:#28780A
}
div#container {
margin: 0 auto;
width:750px;
text-align:left;
}
div#header {
}
div#header h1 {
text-align:center;
}
div#login {
text-align:center;
}
div#footer {
}
div#footer h3 {
text-align:center
}
img {margin:5px 0px}
/* END MAIN */

/* FORM */
form {
width:750px;
margin: 0px auto;
text-align:left;
}
form fieldset {
width:750px;
display:block;
border:1px solid #77B608;
padding:5px;
font-family:verdana, sans-serif;
margin-bottom:0.5em;
line-height:1.5em;
}
form legend {
width:150px;
margin-bottom:5px;
border:2px solid #fff;
padding:3px;
background-color:#E4F8AD;
text-align:center;
font-family:georgia, sans-serif;
font-size:1.1em;
font-weight:bold;
}
form label {
float:left;
width:250px;
margin-right:5px;
line-height:20px;
text-align:right;
}
form label a#refresh {
line-height:40px;

}
form input {
width:250px;
border:1px solid #fff;
padding-left:5px
}
form input#remember {
width:15px;
}
form input#bottom {
width:250px;
margin-top:5px;
background-color:#B4CE06;
color:#FFFFFF;
font-weight:bold
}
br.frmClear {
clear:both
}
span.errors {
color:#FF0000
}
p#invalidLogin {
color:#FF0000
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>TwriTeLy</h1>
</div>
<div id="login">
<form name="frmRegister" action="ServerSideControl" method="post">
<fieldset>
<legend>Register</legend>
<label>Username : </label>
<input type="text" name="username" value="" maxlength="32" /><span
class="errors" ></span><br class="frmClear" />
<label>Password : </label>
<input type="password" name="password" value="" maxlength="32" /><span
class="errors" ></span><br class="frmClear" />
<label>Email : </label>
<input type="text" name="email" value="" maxlength="32" /><span
class="errors" ></span><br class="frmClear" />
<label>&nbsp;&nbsp;</label>
<input id="bottom" type="submit" name="register" value="Register" />
</fieldset>
</form>
</div>
<div id="footer"><h3>By Whisher</h3></div>
</div>
</body>
</html>
TKS A LOT AGAIN !
BYE.

Dec 17 '06 #4
whisher wrote:
<snip>
var usernameError = 'Invalid String username';
<snip ^^^^^^

As an aside, have you thought about the wording of this error message?
You and I may know what a "String" is but what proportion of users can
be expected to know? It is a bit like all the instances you see where
someone will state "javascript required" (or similar), which maybe makes
sense to web developers/designers (even if they often cannot tell Java
from javascript) but is most likely to be gobbledegook to your average
browser uses (who neither know nor care about web technologies).

Richard.
Dec 17 '06 #5

Richard Cornford ha scritto:
whisher wrote:
<snip>
var usernameError = 'Invalid String username';
<snip ^^^^^^

As an aside, have you thought about the wording of this error message?
You and I may know what a "String" is but what proportion of users can
be expected to know? It is a bit like all the instances you see where
someone will state "javascript required" (or similar), which maybe makes
sense to web developers/designers (even if they often cannot tell Java
from javascript) but is most likely to be gobbledegook to your average
browser uses (who neither know nor care about web technologies).

Richard.
I agree with it was only a test.
I'm changing it in the real world ;)
Bye.

Dec 17 '06 #6
In comp.lang.javascript message
<11**********************@l12g2000cwl.googlegroups .com>, Sun, 17 Dec
2006 05:31:03, whisher <wh*****@maktoob.comwrote:
>function isValidEmail(str)
{
var regExEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,4}){1,2}$/;
var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
return (regExEmail.test(str) && !str.match(illegalChars));
};

Unless one is addressing a known closed user group (in which case you
should have said so), it is naive to write a validating RegExp without
an adequate understanding of what is actually valid.

That first RegExp rejects my address for two distinct reasons. It will
also reject any address containing \W characters, such as that of my PC
supplier - and so the second RegExp appears pointless.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ A FAQ for news:comp.lang.javascript.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 18 '06 #7
Hi.
Thanks so much for your useful remarks ;)
and thanks so much again for the useful links.
The new function:
function isValidEmail(str) {
// http://www.devpro.it/php4_id_2.html
return
Boolean(str.match(/^([a-z0-9]+[\._\-]?){1,3}([a-z0-9])*\@([a-z0-9]+[\.\-]?){1,3}([a-z0-9])*\.[a-z]{2,6}$/i));
};
What do you think about it ?
Is your email valid with this ?
Take care.

Ps.
I've to master the regEx ;(

Dec 20 '06 #8
In comp.lang.javascript message
<11**********************@80g2000cwy.googlegroups. com>, Wed, 20 Dec 2006
09:11:03, whisher <wh*****@maktoob.comwrote:
>Thanks so much for your useful remarks ;)
and thanks so much again for the useful links.
Because you have not quoted the preceding article and have not given a
normal attribution, there is no visible indication of who you think you
are talking to.

It's a good idea to read the newsgroup FAQ and RFC1855/FYI28 ...

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 20 '06 #9

Dr J R Stockton ha scritto:
In comp.lang.javascript message
<11**********************@80g2000cwy.googlegroups. com>, Wed, 20 Dec 2006
09:11:03, whisher <wh*****@maktoob.comwrote:
Thanks so much for your useful remarks ;)
and thanks so much again for the useful links.

Because you have not quoted the preceding article and have not given a
normal attribution, there is no visible indication of who you think you
are talking to.

It's a good idea to read the newsgroup FAQ and RFC1855/FYI28 ...

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

Hi.
I told you ;)
Bye.

Dec 26 '06 #10

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

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
4
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...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
16
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...
3
by: Andrea Moro | last post by:
Some times ago I had the same troubles, but I don't remember how, I solved it. Now again. connect to www.comune.crecchio.ch.it/autocertificazione.html and go ahead selecting an item inside the...
2
by: Vinny | last post by:
A customer wants me to convert a VB app I wrote for them into a web app for remote users to do data entry. So, I'm writing a small ASP program which prompts the user for data, much like a VB...
9
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...
27
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...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.