Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Looking for certain strings within a field

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Looking for certain strings within a field

    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???

  2. #2
    Join Date
    Feb 2007
    Location
    Montreal,Quebec
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
      var string = "some string";
      
       if(string.indexOf("some") > -1)
           alert("string found");

  3. #3
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Feb 2007
    Location
    Montreal,Quebec
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    -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

  5. #5
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well now i am going try a few/couply tests to see if i get the whole thing

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Feb 2007
    Location
    Montreal,Quebec
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    its because your code is wrong.
    try the following
    Code:
    <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>

  8. #8
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  9. #9
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

    Code:
    <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>
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  10. #10
    Join Date
    Feb 2007
    Location
    Montreal,Quebec
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •