473,320 Members | 1,831 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,320 software developers and data experts.

need help with form checker

Hello,

can anyone help me write a fragment of code to check to see if textarea form
input contains the string "http" ?

I've successfully created form checking functions with IF statements like
this;

if (document.form1.phone.value == "")
{
alert("Please enter your phone numaber!");
form1.phone.focus();
return false;
}

but I don't have a clue how to parse strings of text input for specific
words or expressions.

thanks for any help with this.

MC
Feb 23 '06 #1
5 1526
VK

Moon Chow wrote:
Hello,

can anyone help me write a fragment of code to check to see if textarea form
input contains the string "http" ?


if (document.forms['form1'].elements['fieldName'].value.indexOf('http')
== -1) {
// ...
}

indexOf(substring) returns the position where the [substring] starts
(first position is 0) or -1 if not found.

If you expect to find an occurence closer to the end, you may use
lastIndexOf(substring) method instead which starts the search from the
end:

if (filePath.lastIndexOf('.gif') == -1) {
alert('Only GIF files are allowed');
}

Feb 23 '06 #2
You can use a regular expression:
http://msdn.microsoft.com/library/en...gexpsyntax.asp
to look for patterns.

function hasHTTP(value)
{
var regx= new RegExp("\bhttp\:\/\/\w","ig"); // looks for
(separator)http://(letter|number) (case insensitive)
return regx.test( value);
}

"http://test" hasHTTP() returns True
"http:// test" hasHTTP() returns False
"in a string of charactershttp://test" hasHTTP() returns False
"http://" hasHTTP() returns False
or if you only want look for "http"
{
var regx= new RegExp("http","g"); // looks for http (case
sensitive)
return regx.test( value);
}

Feb 23 '06 #3
tried your statement in my form-checker like so:

if (document.forms["form1"].elements["name"].value.indexOf("http")
== 0) {
alert("Invalid Data, Please retry...");
form1.name.focus();
return false;
}

But it only caught the "http" if it was at the very begining of the user
input. Is there a variation on this that would catch ANY occurrences of
"http" at any location in the user input string?

Thanks

MC
"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Moon Chow wrote:
Hello,

can anyone help me write a fragment of code to check to see if textarea
form
input contains the string "http" ?


if (document.forms['form1'].elements['fieldName'].value.indexOf('http')
== -1) {
// ...
}

indexOf(substring) returns the position where the [substring] starts
(first position is 0) or -1 if not found.

If you expect to find an occurence closer to the end, you may use
lastIndexOf(substring) method instead which starts the search from the
end:

if (filePath.lastIndexOf('.gif') == -1) {
alert('Only GIF files are allowed');
}


Feb 24 '06 #4
Hi,

thanks for the regx expression; unfortunately I'm a total newbie at JS, and
don't know how to integrate it into my form-checker. could i set up this
function to run "onblur" for each of my form entries, to halt form execution
if it detects "http"...? If so, how can I set up the expression to test
specific field contents; example if the field in question is "form1.name" ?

My normal checking functions follow this syntax:

if (document.form1.name.value == "")
{
alert("Please enter your name.");
form1.name.focus();
return false;
}

Thanks!

MC


<br****@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
You can use a regular expression:
http://msdn.microsoft.com/library/en...gexpsyntax.asp
to look for patterns.

function hasHTTP(value)
{
var regx= new RegExp("\bhttp\:\/\/\w","ig"); // looks for
(separator)http://(letter|number) (case insensitive)
return regx.test( value);
}

"http://test" hasHTTP() returns True
"http:// test" hasHTTP() returns False
"in a string of charactershttp://test" hasHTTP() returns False
"http://" hasHTTP() returns False
or if you only want look for "http"
{
var regx= new RegExp("http","g"); // looks for http (case
sensitive)
return regx.test( value);
}

Feb 24 '06 #5
Moon Chow wrote:
Hi,
Please don't top post. Quote what you are responding too, trim quotes
and put your reply immediately below the quote it refers to.

thanks for the regx expression; unfortunately I'm a total newbie at JS, and
don't know how to integrate it into my form-checker. could i set up this
function to run "onblur" for each of my form entries, to halt form execution
if it detects "http"...? If so, how can I set up the expression to test
specific field contents; example if the field in question is "form1.name" ?


Do not use onblur with an alert for form validation - it annoys the hell
out of users. If you want to report errors while the form is being
completed, write an error message to the page.

And remember that forms must *always* be validated at the server,
client-side validation is unreliable and is only for user convenience.
e.g.

<title>Error message play</title>

<style type="text/css">
.errMsg {color: red; font-weight: bold; background-color: #fee;}
</style>

<script type="text/javascript">

function hasStr(srcString, testString)
{
var re = new RegExp(testString, 'i');
return (re.test(srcString));
}

function checkStr(el, str, bool)
{
if (el.value && hasStr(el.value, str) != bool){
showErr(el, 'Input must ' + ((bool)?'':'not')
+ ' contain ' + str);
} else {
showErr(el, '');
}
}

function showErr(el, erString)
{
if (el.name && document.getElementById){
var erEl = document.getElementById(el.name + '.msg');
if (erEl) erEl.innerHTML = erString;
}
}

</script>
<div>
<label for="i-01">This input must not have 'http'<br>
<input name="i-01"
onblur="checkStr(this,'http',false);"></label><span
class="errMsg" id="i-01.msg"></span><br>
<label for="i-02">This input must have 'Nancy'<br>
<input name="i-02"
onblur="checkStr(this,'Nancy',true);"></label><span
class="errMsg" id="i-02.msg"></span>
</div>
The above functions would normally be put inside a single object and
called as part of a much more generic form validation process, but you
get the idea.

[...]

--
Rob
Feb 24 '06 #6

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

Similar topics

0
by: Jim | last post by:
here is the problem.. When this procedure runs I am supposed to get 2 output parameters. When an insert statement is going to generate duplicate names I get 1 for @checker and 3 for @insertid. ...
20
by: Arne | last post by:
During testing <div style="overflow:auto;"> in CSS I noticed the mousewheel would work in Mozilla only after I made a <a href="#">some text</a> link and clicked on that, within the div. It...
19
by: tthunder | last post by:
Hi @all, I've got an interesting problem. These are my classes: ---------------------- class fooBase {
6
by: zzapper | last post by:
Hi, If I make a mistake designing a JavaScript Form Checker, ie get the name (or case) of a form Field wrong, JavaScript seems to default to immediately returning true and the form is submitted...
0
by: Stylus Studio | last post by:
World's Most Advanced XML Schema Editor Adds Support for IBM AlphaWorks XML Schema Quality Checker to Improve XML Schema Style and Quality BEDFORD, MA -- 09/13/2005 -- Stylus Studio...
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
4
by: sweetguy1only | last post by:
Hi all, I am a MS Access developer using VB 6 (yes, I know it is a bit old). The problem I am having is, I have a software that allows my customers to put in the information of their clients....
3
by: =?gb2312?B?zfW2rA==?= | last post by:
I need a C++ code style checker. Which is the best choice? free or for fee.
3
by: Kinokunya | last post by:
Hi guys, My group and I will be working on our final year project, the scope to do a program/web-based application similar areas of functionalities like the PyLint and PyChecker; a Python syntax...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.