Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > need help with form checker

Reply
Thread Tools

need help with form checker

 
 
Moon Chow
Guest
Posts: n/a
 
      02-23-2006
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


 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      02-23-2006

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');
}

 
Reply With Quote
 
 
 
 
brattn@gmail.com
Guest
Posts: n/a
 
      02-23-2006
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);
}

 
Reply With Quote
 
Moon Chow
Guest
Posts: n/a
 
      02-24-2006
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" <> wrote in message
news: 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');
> }
>




 
Reply With Quote
 
Moon Chow
Guest
Posts: n/a
 
      02-24-2006
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






<> wrote in message
news: 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);
> }
>



 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      02-24-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help in writing up a Python Syntax checker Kinokunya Python 3 07-31-2008 02:29 PM
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
MSN Messenger Block Checker and Yahoo Block Checker mianriz Software 0 07-30-2006 08:22 AM
MSN BLOCK CHECKER-MSN STATUS CHECKER-MSN PROBLEMS Pager O Rama Digital Photography 0 04-04-2006 06:58 PM
MSN BLOCK CHECKER-MSN STATUS CHECKER-MSN PROBLEMS Pager O Rama ASP General 0 04-04-2006 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57