473,467 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Javascript: string detection

Hello Group:

I'd be grateful to anyone who can help me with Javascript form. :-)

I want users to submit a web form, but to stop the form submission if
any of my text fields contain specific value (e.g. "http").
I incorporated "http" detection bits of script into my existing
function (only for one of my fields for now ), but for some reason this
script doesn' work, probably, because of the wrong syntax.
=============
function checkEmail(theForm)
{
// Customize these calls for your form
// Start ------->
if (!validEmail(theForm.Email,"Email Address",true))
return false;
// <--------- End

if (document.InqServicesForm.FName.value.indexOf('htt p') -1)
return false;
{
alert("http detected.");
return false;
}

return true;
}
=============

Could you please have a look at my form script?

The form's URL is here:
http://www.ccohs.ca/ccohs/inquiries_...ries_form.html

+++++++++++++++++
function isEmailAddr(Email)
{
var result = false;
var theStr = new String(Email);
var index = theStr.indexOf("@");
if (index 0)
{
var pindex = theStr.indexOf(".",index);
if ((pindex index+1) && (theStr.length pindex+1))
result = true;
}
return result;
}

function isEmail(string) {

if (!string) return false;
var iChars = "*|,\":<>[]{}`\';()&$#%";

for (var i = 0; i < string.length; i++) {
if (iChars.indexOf(string.charAt(i)) != -1)
return false;
}
return true;
}

function validEmail(formField,fieldLabel,required)
{
var result = true;
if (result && ((formField.value.length < 3) ||
!isEmailAddr(formField.value)))
{
alert("Please enter a complete email address in the form:
yo******@yourdomain.com");
formField.focus();
result = false;
}

return result;

}

function checkEmail(theForm)
{
// Customize these calls for your form
// Start ------->
if (!validEmail(theForm.Email,"Email Address",true))
return false;
// <--------- End

if (document.InqServicesForm.FName.value.indexOf('htt p') -1)
return false;
{
alert("http detected.");
return false;
}

return true;
}

document.forms["InqServicesForm"]

function checkForm(){
var w=document.forms["InqServicesForm"];
if(
w.FName.value == '' ||
w.LName.value == '' ||
w.Phone.value == '' ||
w.Address.value == '' ||
w.City.value == '' ||
w.Province.value == '' ||
w.PostalCode.value == '' ||
w.Email.value == ''
)
{
alert("Please fill in all required fields.");
return false;
}
return true;

}
+++++++++++++++++++

Many thanks.
O.

Oct 31 '06 #1
5 2739
Hello again:

I bugged you for nothing, guys. Sorry. I got it to work.

Regards.
O.

Oct 31 '06 #2
In message <11**********************@m73g2000cwd.googlegroups .com>, Tue,
31 Oct 2006 11:33:14, Olly <ol******@yahoo.comwrites
>
I want users to submit a web form, but to stop the form submission if
any of my text fields contain specific value (e.g. "http").
>if (document.InqServicesForm.FName.value.indexOf('htt p') -1)
(A) What is the objection to Mr. Srinavasa Tusithttpuniwala entering his
name?
(B) Do you really want to allow "httP://www.pawnshop.om/" to be entered?

Text field validation is, almost invariably, best done by using RegExps
: see <URL:http://www.merlyn.demon.co.uk/js-valid.htmand references
therein.

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/>? JL/RC: FAQ of 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.
Nov 1 '06 #3
Olly wrote:

[snip]
I incorporated "http" detection bits of script into my existing
function (only for one of my fields for now ), but for some reason
this script doesn' work, probably, because of the wrong syntax.
Please describe what "doesn't work" means in this case. What were you
expecting to see and what exactly should cause it?

Might I ask why "http" is so important? I can think of many acronyms and
abbreviations that wouldn't be relevant in a contact form, yet none of
them are worth special treatment. If it breaks something server-side,
then it is that server-side process which should be fixed. Client-side
scripting isn't a solution.
function checkEmail(theForm)
{
// Customize these calls for your form
// Start ------->
if (!validEmail(theForm.Email,"Email Address",true))
return false;
// <--------- End
An e-mail validation function that calls another function that calls yet
another function seems really quite excessive.
if (document.InqServicesForm.FName.value.indexOf('htt p') -1)
Moreover, if you're going start checking other fields, checkEmail isn't
really a suitable name any more.
return false;
{
alert("http detected.");
return false;
}
This is curious: you have a block statement without an associated
control statement. Legal certainly, but probably not what you intended.
Remove the first return statement.
For actually validating an e-mail address,

function isEmailAddress(string) {
return /^[^@]+@([^.]+\.)+[a-z]+$/.test(string);
}

should suffice. Anything more restrictive than that is a waste of time.
A complete syntax check is better left to the server where existing code
is more readily available and not a burden on the client. Even then, the
only way to determine if an e-mail address exists is to send mail to it
and get the user to respond in some way.

Be aware that even the regular expression above is restrictive as it
won't accept literal IPv6 addresses, those that contain display names,
or comments in some locations. I should think that the former would be
too impractical to remember, and a host name that resolved to an IPv6
address would be more likely. Leading or trailing comments are more
likely in addresses that included a display name, and you don't want
display names, anyway, just the address specification (addr-spec).

[snip]

Mike
Nov 1 '06 #4
In message <r2*******************@text.news.blueyonder.co.uk> , Wed, 1
Nov 2006 18:53:11, Michael Winter <m.******@blueyonder.co.ukwrites
>
For actually validating an e-mail address,

function isEmailAddress(string) {
return /^[^@]+@([^.]+\.)+[a-z]+$/.test(string);
}

should suffice.
An E-address is of the form LHS@RHS. The second @ in your RegExp seems
to represent the one between LHS & RHS, and the first @ in the RegExp
ensures that the second @ in the RegExp finds the first @ in the string.
But I see nothing to ensure that the apparent RHS contains no @ -
indeed, "a@b@c.d" is accepted. Change [^.] to [^.@] ?

Personally, I think that the purpose of the test must be to ensure that
the field is non-empty, and that what it contains is an attempted
E-address rather than something which should have been put elsewhere.
For that, <something@ <something. <something seems sufficient.
>Be aware that even the regular expression above is restrictive as it
won't accept literal IPv6 addresses, those that contain display names,
or comments in some locations. I should think that the former would be
too impractical to remember, and a host name that resolved to an IPv6
address would be more likely. Leading or trailing comments are more
likely in addresses that included a display name, and you don't want
display names, anyway, just the address specification (addr-spec).
Redefine my "<something>" from .+ to \S+ (a good move anyway!), omit
the leading ^ & trailing $, and it will accept anything containing a
"word" which might be an E-address.

In general, ISTM that one can do an easy minimal test to show that it is
an attempted E-address, or a difficult maximal test to assure full
compliance with one of the forms permitted by RFCs --- and that anything
in between smacks of falling between two stools.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of 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.
Nov 2 '06 #5
Dr J R Stockton wrote:
In message <r2*******************@text.news.blueyonder.co.uk> , Wed, 1
Nov 2006 18:53:11, Michael Winter <m.******@blueyonder.co.ukwrites
>For actually validating an e-mail address,

function isEmailAddress(string) {
return /^[^@]+@([^.]+\.)+[a-z]+$/.test(string);
}

should suffice.

An E-address is of the form LHS@RHS. The second @ in your RegExp seems
to represent the one between LHS & RHS, and the first @ in the RegExp
ensures that the second @ in the RegExp finds the first @ in the string.
But I see nothing to ensure that the apparent RHS contains no @ -
indeed, "a@b@c.d" is accepted. Change [^.] to [^.@] ?
It depends on what the real aim is, I suppose. On reflection, I hadn't
adequately determined that for myself.

I think it's reasonable that exactly one at symbol (@) should occur in
an e-mail address, even though more can occur if quoted strings
(local-part), comments, or domain-literals are permitted. Further, if
only the address specification is needed (display name and such are to
be rejected) then the domain part could be limited to legal domain names
(and possibly IP addresses).
Personally, I think that the purpose of the test must be to ensure that
the field is non-empty, and that what it contains is an attempted
E-address rather than something which should have been put elsewhere.
For that, <something@ <something. <something seems sufficient.
Maybe. The address should be completely examined before use, so
something as lax as /.+@.+(\..+)+/ would be fine in the end. That said,
it's astonishing how cavalier some people are regarding form mail
validation. Only today did I see someone using absolutely no syntax
checking at all.

[snip]
Redefine my "<something>" from .+ to \S+ (a good move anyway!), omit
the leading ^ & trailing $, and it will accept anything containing a
"word" which might be an E-address.
Yes, though there are legal addresses that could contain spaces, not
that I'd expect them to be in use. That change would still validate such
an address, though it wouldn't match the entire thing.
In general, ISTM that one can do an easy minimal test to show that it is
an attempted E-address, or a difficult maximal test to assure full
compliance with one of the forms permitted by RFCs --- and that anything
in between smacks of falling between two stools.
Not necessarily. I think there is a middle ground, it's just a matter of
finding it.

I have encountered ridiculously restrictive interfaces: one wouldn't
allow full stops (.) in the local-part, and one wouldn't allow
underscores (_). Naturally, in each instance I wanted to use an address
that contained such characters. Whilst I would hope that no-one in their
right mind would ever want to reject those characters, and though I
don't think narrowing down the precise set of allowable characters is
sensible either, something as simple as limiting the presence of the at
symbol and dot doesn't quite deserve to be labelled a half-hearted attempt.

Clearly, if one is expecting a full mailbox or mailbox list, then only a
minimal test is practical.

Mike
Nov 3 '06 #6

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

Similar topics

72
by: Stephen Poley | last post by:
I have quite often (as have probably many of you) come across HTML forms with irritating bits of Javascript attached. The last straw on this particular camel's back was a large form I was asked 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
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...
1
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...
0
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...
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,...
1
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...
0
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...

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.