Results 1 to 6 of 6

Thread: A code that detects if a value start with something (?)

  1. #1
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A code that detects if a value start with something (?)

    A textfield has the value "http://www.google.com/".
    I want to have a javascript that detects if the value starts with "http://", because then it will do "alert('The value starts with http://')". Or if the textfield's value is something else, it should do "alert('Nothing special...')"

    How to do this?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    String.prototype.startsWith = function(s) {
        return this.substr(0, s.length) === s;
    };
    Code:
    "http://www.google.com/".startsWith("http://") // true
    "http://www.google.com/".startsWith("google") // false
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I don't get it.
    Where do I put this?

    Or should i do like this?
    Code:
    function goButton(){
    	String.prototype.startsWith = function(s){
    		return this.substr(0, s.length) === s;
    	}
    	if(document.getElementById('addressBar').value.startsWith == "http://"){
    		alert('The value starts with http://');
    	}
    }
    I don't think so... But I don't understand what you mean by just posting the code :S
    But i'm appreciating your help

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Implementation from Twey's prototype:
    Code:
    <script type="text/javascript">
    String.prototype.startsWith = function(s) {
        return this.substr(0, s.length) === s;
    };
    function goButton(el,string)
    {
    alert(document.getElementById(el).value.startsWith(string)?'The value starts with '+string:'It doesn\'t starts with '+string);
    }
    </script>
    <input type="text" id="inp"><input type="button" onclick="goButton('inp','http://')" value="Check Start">
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    String.prototype.startsWith = function(s){
        return this.substr(0, s.length) === s;
    };
    
    function goButton() {
        if (document.getElementById("addressBar").value.startsWith("http://"))
            alert("The value starts with http://");
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    yeah thx, i figured it out myself
    thx guys!

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
  •