By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

Form validation question

P: n/a
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 lost.

Thanks for reading!
Oct 22 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Hosh wrote on 22 okt 2005 in comp.lang.javascript:
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?
..............
function validate(x){
// this depends on your requirements
firstInput = x.blah.value
................
}
</script>

............
<form onsubmit='return validate(this)'>
<input name='blah' ..........
I'm a total JavaScript newbie and am completely lost.


Learn or hire someone. Validating is not a beginners job.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 22 '05 #2

P: n/a
Thanks for that.
Learn or hire someone. Validating is not a beginners job. That's the plan (learn). That's why I'm here.
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.242... Hosh wrote on 22 okt 2005 in comp.lang.javascript:
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?


.............
function validate(x){
// this depends on your requirements
firstInput = x.blah.value
................
}
</script>

...........
<form onsubmit='return validate(this)'>
<input name='blah' ..........
I'm a total JavaScript newbie and am completely lost.


Learn or hire someone. Validating is not a beginners job.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 22 '05 #3

P: n/a
Hosh wrote on 23 okt 2005 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
Hosh wrote on 22 okt 2005 in comp.lang.javascript:
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?
.............
function validate(x){
// this depends on your requirements
firstInput = x.blah.value
................
}
</script>

...........
<form onsubmit='return validate(this)'>
<input name='blah' ..........
I'm a total JavaScript newbie and am completely lost.


Learn or hire someone. Validating is not a beginners job.


[first thing to learn: do not toppost on usenet, please]
Thanks for that.
Learn or hire someone. Validating is not a beginners job.

That's the plan (learn). That's why I'm here.


Then, we are not going to do your job in programming, doing sonone elses
programming is a professional job, but if you supply a bit of code you
are troubled with, you usually will get all the help you could wish.

As I showed you above, set your goals in validation and try to make some
code, test it in the framework I showed bove, or a better plan, I you
have one, and test it.

Then return with your problems.

Did you read this NG's FAQ and archive yet?

primary faq:
<http://jibbering.com/faq/>

secondary faq:
<http://www.merlyn.demon.co.uk/js-index.htm>

archive:
<http://groups.google.com/group/comp.lang.javascript>
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 22 '05 #4

P: n/a
Obviously, I wandered into the wrong newsgroup.
I was only looking for some advice.
Oct 23 '05 #5

P: n/a
Hosh said the following on 10/22/2005 8:13 PM:
Obviously, I wandered into the wrong newsgroup.
I was only looking for some advice.


Please quote what you are replying to.

And that is what you got. Some advice. And it happened to be good advice
at that. If you came wanting someone to write a complete validation
script for you, without you even trying, then yes you can to the wrong
newsgroup.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 23 '05 #6

P: n/a
"Hosh" <1@2.com> writes:
I have a form on a webpage and want to use JavaScript validation for the
form fields.
Ok. First describe, in words, which values are acceptable for which
fields, and what should happen if the values fail to validate.
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".
It's dangerous to use other people's validation scripts without
understanding them, because they might not do exactly what you want.
E-mail validation is a case where many scripts you find are too
restrictive, and not accepting a user's e-mail is a pretty sure way
to lose a customer (if one's site has such). Likewise phone number
validation should probably be prepared for foreign numbers as well.
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?
Most likely the latter. Each validation script consistes of some
scaffolding that calls the script at certain times, some actual
tests on values, and a response if validation fails. You only
want the scaffolding and response once, but you want a test
for each value that you have requirements for.
I'm a total JavaScript newbie and am completely lost.


Ok, here is a template for making a validation script:

---
<script type="text/javascript">
/* a validation function called when the user tries to submit the form */
function validate(form) {
var success = true;
// for each value to validate:
var field1Value = form.elements['field1'].value;
if (! ... some test on the value ...) {
success = false;
// possibly note which field fails for the user.
}
// end for each
if (!success) {
// tell the user somehow, e.g., alert("Invalid values entered")
// but preferably a more descriptive text.
}
return success;
}
</script>
.....
<form action=".." onsubmit="return validate(this);">
... <input type="text" name="field1" ... > ...
</form>

---

Such a validation script checks only when the user tries to submit.
It's also possible to validate a field as soon as the user leaves it
(or even when he presses a key, but that's usually considered too
intrusive), but what code you need depends on the description you
started out with of what it should do.
Also, never forget that client-side validation is only a convenience
to prevent a server round-trip that would fail anyway. The server must
always validate its input, whether the client does so or not.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Oct 23 '05 #7

P: n/a

"Randy Webb" <Hi************@aol.com> wrote in message
news:_-********************@comcast.com...
Hosh said the following on 10/22/2005 8:13 PM:
Obviously, I wandered into the wrong newsgroup.
I was only looking for some advice.


Please quote what you are replying to.

And that is what you got. Some advice. And it happened to be good advice
at that. If you came wanting someone to write a complete validation script
for you, without you even trying, then yes you can to the wrong newsgroup.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly

You know, in my original post, my only question was:
"Is it ok to put all these scripts on one page as they are or should they be
joined in some way together to be one script?"

I don't really see any request whatsoever for someone to write a damned
script for me!
You should wander into some friendly newsgroups and see how they treat
people that are just trying to get their feet wet.
Sorry for the intrusion!
Oct 23 '05 #8

P: n/a
Hosh said the following on 10/22/2005 9:23 PM:
"Randy Webb" <Hi************@aol.com> wrote in message
news:_-********************@comcast.com...
Hosh said the following on 10/22/2005 8:13 PM:
Obviously, I wandered into the wrong newsgroup.
I was only looking for some advice.
Please quote what you are replying to.

And that is what you got. Some advice. And it happened to be good advice
at that. If you came wanting someone to write a complete validation script
for you, without you even trying, then yes you can to the wrong newsgroup.


You know, in my original post, my only question was:
"Is it ok to put all these scripts on one page as they are or should they be
joined in some way together to be one script?"


Yes, you should combine them. But to be able to combine them you have to
have an understanding of what you are combining.
I don't really see any request whatsoever for someone to write a damned
script for me!
Fair enough.
You should wander into some friendly newsgroups and see how they treat
people that are just trying to get their feet wet.


You mean we aren't friendly? Awww shucks.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 23 '05 #9

P: n/a
Lee
Hosh said:


"Randy Webb" <Hi************@aol.com> wrote in message
news:_-********************@comcast.com...
Hosh said the following on 10/22/2005 8:13 PM:
Obviously, I wandered into the wrong newsgroup.
I was only looking for some advice.


Please quote what you are replying to.

And that is what you got. Some advice. And it happened to be good advice
at that. If you came wanting someone to write a complete validation script
for you, without you even trying, then yes you can to the wrong newsgroup.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly

You know, in my original post, my only question was:
"Is it ok to put all these scripts on one page as they are or should they be
joined in some way together to be one script?"

I don't really see any request whatsoever for someone to write a damned
script for me!


Imagine somebody dropping by a forum of electrical contractors and
saying that they found some wires in their wall, and they wonder if
they should just connect them together.

You might get the impression that they're an unfriendly lot.
In the first place, we can't tell you if code that we've never seen
can be easily combined into a single validation function.

Then there's the whole problem of the trouble that you can get into
working with code that you don't understand.

Whether or not it's really dangerous depends on what sort of data
you're collecting, how you're transmitting it, and what you're doing
with it on the back-end.

Oct 23 '05 #10

P: n/a
Hosh wrote:
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 lost.

Thanks for reading!

First, Javascript form data validation is a misused term. You can make
sure submitted data is valid by utilizing client side technologies (i.e.
JavaScript). They can be fooled, disabled, bypassed, etc. Data
validation should be the job of the server side script.
Client side is useful in helping users fill the form, saving their time
and server bandwidth by pointing out errors and omissions. When both are
implemented properly, some functionality may overlap, but it will not be
all the same.

That said, google "fValidate"

--
Vladdy
http://www.klproductions.com
Oct 23 '05 #11

P: n/a

"Vladdy" <vl**@klproductions.com> wrote in message
news:SXC6f.2432$c4.1437@trndny03...
Hosh wrote:
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 lost.

Thanks for reading!

First, Javascript form data validation is a misused term. You can make
sure submitted data is valid by utilizing client side technologies (i.e.
JavaScript). They can be fooled, disabled, bypassed, etc. Data validation
should be the job of the server side script.
Client side is useful in helping users fill the form, saving their time
and server bandwidth by pointing out errors and omissions. When both are
implemented properly, some functionality may overlap, but it will not be
all the same.

That said, google "fValidate"

--
Vladdy
http://www.klproductions.com


Thanks.
I am aware of the potential problems with client-side validation, including
the fact that 10% of users have Javascript turned off in their browsers.
I do in fact have server-side data validation going on. I just wanted to
have some Javascript validation for exactly the reasons that you've outlined
above. The form I have is large and was hoping to alert the user to errors
as they went along as opposed to when they clicked the submit button.
Thanks again.
Oct 23 '05 #12

P: n/a
Hosh wrote:
[...]
I am aware of the potential problems with client-side validation,
including the fact that 10% of users have Javascript turned off in their
browsers.


Your numbers are -- as always with so-called statistics,
without reliable representative data to base on -- wrong.

And please learn how to quote properly: <http://jibbering.com/faq/>
PointedEars
Oct 23 '05 #13

P: n/a
Thomas 'PointedEars' Lahn said the following on 10/23/2005 4:50 AM:
Hosh wrote:

[...]
I am aware of the potential problems with client-side validation,
including the fact that 10% of users have Javascript turned off in their
browsers.

Your numbers are -- as always with so-called statistics,
without reliable representative data to base on -- wrong.


Do you have firsthand knowledge that he *doesnt* have "reliable
representative data" to base that 10% on? Unless you do, you can in *no
way* say it is wrong.
And please learn how to quote properly: <http://jibbering.com/faq/>


There was nothing wrong with the way Hosh quoted short of his news agent
not trimming the signature. But the quoting itself was fine.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 23 '05 #14

P: n/a
"Hosh" <1@2.com> writes:
The form I have is large and was hoping to alert the user to errors
as they went along as opposed to when they clicked the submit button.


In that case, what you want is not really "form validation" as much
as "field validation", since you validate each data field for itself.

It's more likely that you can combine several individual field
validation scripts on a page without changing them (as long as there
is no name collision) than that you could do the same for form
validations, since they are run independently.

A template for a single field validation would be something like:

---
<script type="text/javascript">
function validateSomeProperty(field) {
var value = field.value;
if (!... some test on value...) {
// report error, e.g.,
field.style.borderColor = "red";
// or : alert("Field foo contains invalid character"), or ...

// possibly fix entry (e.g., date into consisten format)
}
}
</script>
...
<form ...>
... <input type="text" name="email"
onchange="validateSomeProperty(this)"> ...
</form>
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Oct 23 '05 #15

P: n/a
JRS: In article <SXC6f.2432$c4.1437@trndny03>, dated Sun, 23 Oct 2005
02:54:42, seen in news:comp.lang.javascript, Vladdy
<vl**@klproductions.com> posted :
First, Javascript form data validation is a misused term. You can make
sure submitted data is valid by utilizing client side technologies (i.e.
JavaScript). They can be fooled, disabled, bypassed, etc. Data
validation should be the job of the server side script.
Client side is useful in helping users fill the form, saving their time
and server bandwidth by pointing out errors and omissions. When both are
implemented properly, some functionality may overlap, but it will not be
all the same.

Don't presume that all processing is always carried out on the server,
or that any data is necessarily returned to the server. Javascript has
applications other than commercial.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 23 '05 #16

P: n/a
Dr John Stockton wrote on 23 okt 2005 in comp.lang.javascript:
JRS: In article <SXC6f.2432$c4.1437@trndny03>, dated Sun, 23 Oct 2005
02:54:42, seen in news:comp.lang.javascript, Vladdy
<vl**@klproductions.com> posted :
First, Javascript form data validation is a misused term. You can make
sure submitted data is valid by utilizing client side technologies (i.e.
JavaScript). They can be fooled, disabled, bypassed, etc. Data
validation should be the job of the server side script.
Client side is useful in helping users fill the form, saving their time
and server bandwidth by pointing out errors and omissions. When both are
implemented properly, some functionality may overlap, but it will not be
all the same.

Don't presume that all processing is always carried out on the server,
or that any data is necessarily returned to the server. Javascript has
applications other than commercial.


True.

If I want to do maintenance on my own on server databases from clientside
with a well password [and IP tested] protected page, clientside string
input validation is just as safe [and much faster].

"Hacking you own site" is a contradictio in terminis.
JRS: In article <SXC6f.2432$c4.1437@trndny03>, dated Sun, 23 Oct 2005
client side technologies (i.e. JavaScript).


Only partly. I use serverside javascript too.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 24 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.