PDA

View Full Version : Looking for certain strings within a field



pcbrainbuster
02-26-2007, 05:58 PM
Hello all:),

I have seen on the web certain fields that for example ask for your email and if it does not include a certain string/number... then it is not sent.

All i know that indexOf is used for this but i do not know how to use it :(
Can anyone help me with my problem???

CrazyCoder
02-26-2007, 06:04 PM
var string = "some string";

if(string.indexOf("some") > -1)
alert("string found");

pcbrainbuster
02-26-2007, 06:09 PM
Thanks for your post :)

But there are only two things i do not understand how to set it to search 2 or more values/strings and what is that part > -1 does that say if the value/string(s) are there then do the following ?

Please explain these area and go deep into > -1 Thanks :)

CrazyCoder
02-26-2007, 06:13 PM
-1 is returned when the script cannot find the index of the string asked for so im checking if my value returned is higher then -1

if you want to dig deeper and find more solutions to finding strings.

visit the following site http://www.javascriptkit.com/javatutors/string4.shtml

pcbrainbuster
02-26-2007, 06:22 PM
Well now i am going try a few/couply tests to see if i get the whole thing :)

pcbrainbuster
02-26-2007, 06:35 PM
Hello again :),

I tried a test and failed please tell me what went wrong -

<html>
<body>
<input type="text" id="text1"><input type="button" onclick="asd()" value="Send">
<script>
var a = document.getElementById('text1').value
function asd() {
if (a.indexOf("q") > -1) {
alert("Sent\!\!\!")}
else {alert("Sorry Invalid Entry")}
}
</script>
</body>
</html>

Thanks :)

CrazyCoder
02-27-2007, 02:13 AM
its because your code is wrong.
try the following


<html>
<head>
<script type="text/javascript" language="JavaScript">
function asd() {
//you got the control ok but not getting the value when the user clicked this is correct
var a = document.getElementById('text1').value
if (a.indexOf("q") > -1) {
//no clue why you were escaping your !
alert("Sent ! ! !")}
else {alert("Sorry Invalid Entry")}
}
</script>
</head>
<body>
<input type="text" id="text1"><input type="button" onclick="asd()" value="Send">
</body>
</html>

pcbrainbuster
02-27-2007, 07:32 AM
Well there is a problem your script, works correctly but most sites say the the variables should always be put before the function but in our scrpts that is the only difference mine is above your is under...

Please explain about thus :)
Thanks

Blake
02-27-2007, 12:51 PM
In your version, the variable a is assigned the value of the field text1 when the page loads (nothing). You need to assign to a the value of the field text1 when the button is clicked, not when the page loads.

There's nothing wrong with declaring the variable inside the function. It will just be local to the function then. If you want a to be a global variable (I don't see any reason to), you would need to do something like this:



<html>
<head>
<script type="text/javascript" language="JavaScript">
var a;

function asd()
{
a = document.getElementById('text1').value;
if (a.indexOf("q") > -1) alert("Sent ! ! !");
else alert("Sorry Invalid Entry");
}
</script>
</head>
<body>
<input type="text" id="text1"><input type="button" onclick="asd()" value="Send">
</body>
</html>

CrazyCoder
02-27-2007, 03:32 PM
Thank you for clearing this up Black :)

pcbrainbuster I think you should propably read a book on javascript not saying it's bad to jump in and try but get a basic idea of how things work.

pcbrainbuster
02-27-2007, 04:21 PM
Thanks Blake for the info :)

And YOU CRAYZCODER I only ask alot of questions so that i can build up some sort of insurance if you know what i mean :)

Well i know understand the use of indexof but there is only jst only one more thing how to assighn more than one value in the indexOf thing do you use the character ,

Anyway you said that i did not need to assign a escape to ! well i only added it because i was not sure if ! needed the \ well it does not matter anyway does it?

Blake
02-27-2007, 04:26 PM
Thanks Blake for the info :)

And YOU CRAYZCODER I only ask alot of questions so that i can build up some sort of insurance if you know what i mean :)

Well i know understand the use of indexof but there is only jst only one more thing how to assighn more than one value in the indexOf thing do you use the character ,

Anyway you said that i did not need to assign a escape to ! well i only added it because i was not sure if ! needed the \ well it does not matter anyway does it?

I don't follow what you're asking. What do you mean by "assign more than one value in the indexOf thing?"

By the way, check out the bar graph thing I added to my website (it works when you hover over one of the links). :)

jscheuer1
02-27-2007, 04:37 PM
One could test() (http://msdn2.microsoft.com/en-us/library/a55e5s6b.aspx) or 'test' using indexOf() repeatedly against an array of items to check for each item.

pcbrainbuster
02-27-2007, 04:37 PM
Congratualtions very good site :)

Well what i meant by the value thing is that say i wanted to find @ in a string i would do this str.indexOf("@")

but what if i wanted to search for two values i would do str.indexOf("@,A") something like that - or is that correct because it was just an example.

I know that i could always use indexOf twice but it is better to do it with 2-in-1 values if you know what i mean:)

jscheuer1
02-27-2007, 04:41 PM
/@|A/.test(str) will return true if either @ or A is present.

/@.*A|A.*@/.test(str) requires that both be present to return true.

pcbrainbuster
02-27-2007, 04:43 PM
Well you lost me but i think i understand is this what you mean ? -

str.indexOf("@||A")

Blake
02-27-2007, 05:02 PM
Well you lost me but i think i understand is this what you mean ? -

str.indexOf("@||A")

No, he's using regular expressions.

pcbrainbuster
02-27-2007, 05:03 PM
Errrr, whats that mean :confused:

Blake
02-27-2007, 05:05 PM
http://www.greggriffiths.org/webdev/clientside/javascript/regex/index.html

jscheuer1
02-27-2007, 05:10 PM
Well you lost me but i think i understand is this what you mean ? -

str.indexOf("@||A")


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function check(str){
//will return true if either @ or A is present.
alert( /@|A/.test(str)? "You've clicked one with A or @ or both" : "Neither A nor @ present" );
//requires that both be present to return true.
alert( /@.*A|A.*@/.test(str)? "You've clicked one with A and @" : "Either A or @ or both are not present" );
}

</script>
</head>
<body>

<a href="#" onclick="check(this.innerHTML);return false;">Both @ and A</a><br>
<a href="#" onclick="check(this.innerHTML);return false;">Only @ here</a><br>
<a href="#" onclick="check(this.innerHTML);return false;">None here</a><br>
Try your own string:
<input id="chk" type="text"><input type="button" onclick="check(document.getElementById('chk').value);" value="Check It">

</body>
</html>

pcbrainbuster
02-27-2007, 05:47 PM
WOW, regular expressions seem to be a ver y complex thing but for now i just wish to focus on the indexof thing -

So any ideas how to search for 2 values in one indexof????:(

pcbrainbuster
02-27-2007, 06:23 PM
Urrrr,
Guys were have you lot gone to ?

Blake
02-27-2007, 06:59 PM
I think the best you can do is

str.indexOf("x") != -1 || str.indexOf("y") != -1

or

str.indexOf("x") != -1 && str.indexOf("y") != -1

pcbrainbuster
02-27-2007, 07:10 PM
Yeah well that part was already figured but thanks for your posts - i could have never done this without you (unless someone else came along) :)