need help with form checker

M

Moon Chow

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
 
V

VK

Moon said:
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');
}
 
B

brattn

You can use a regular expression:
http://msdn.microsoft.com/library/en-us/script56/html/js56jsgrpregexpsyntax.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);
}
 
M

Moon Chow

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 said:
Moon said:
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');
}
 
M

Moon Chow

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
 
R

RobG

Moon said:

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.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top