Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    filtering form input - going wrong

    Hi been away for a while and have gone a bit rusty.

    i am running a form where the url is to be entered. I want to clear out the http:// and the www., which if done, would then show the remainder of the url in the textbox.

    I can seem to recall how to wrap the unwanted set of characters into the script.

    what have I forgotten?

    Code:
    //check the web_url
    		var val = document.getElementById("myForm").web_url.value;
    		val = val.replace(/^\\s+|\\s+\$/g,""); // strip all leading and trailing spaces
            val = val.replace(/\\s{2,}/g," ");  // convert multiple spaces into a single space
    		val = val.replace (/[^a-z\\s\\-\.]/gi,"");  // strip the characters from the url that are not allowed
    		val = val.replace (/http/gi, "") // strip the http
    		val = val.replace (/www./gi, "") // strip the www.
            document.getElementById("myForm").web_url.value = val; // write it back to the field
    		if (!(/^([a-z0-9])([\\w\\.\\-\\+])+([a-z0-9]?)+([a-z]{2,4})\$/i.test(val))) {
                //alert( "Please enter a valid web address." );
                document.getElementById("urlerr").innerHTML ="Please enter a valid web address, without the http:// and without the www.";
                document.getElementById("myForm").web_url.value= "";  // clear the field
                setTimeout("document.getElementById('myForm').web_url.focus()", 25); // refocus on it
                return false;
            } 
    		else {
                document.getElementById("urlerr").innerHTML ="";
            }
    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    oh, I think I got it. However, just to get it right, this version would not allow "www.www.com" [without quotes] because the result alllowed in the form would then be 'com' [without quotes]

    Code:
    //check the web_url
    		var val = document.getElementById("myForm").web_url.value;
    		val = val.replace(/^\\s+|\\s+\$/g,""); // strip all leading and trailing spaces
            val = val.replace(/\\s{2,}/g," ");  // convert multiple spaces into a single space
    		val = val.replace (/[^a-z0-9\\s\\-\.\:]/gi,"");  // strip the characters that cannot appear in a url
    		val = val.replace (/http\:/gi, "") // strip the http
    		val = val.replace (/www\./gi, "") // strip the www.
            document.getElementById("myForm").web_url.value = val; // write it back to the field
    		if (!(/^([a-z0-9])([\\w\\.\\-\\+])+([a-z0-9]?)+([a-z]{2,4})\$/i.test(val))) {
                //alert( "Please enter a valid web address." );
                document.getElementById("urlerr").innerHTML ="Please enter a valid web address, without the http:// and without the www.";
                // document.getElementById("myForm").web_url.value= val;  // clear the field
                setTimeout("document.getElementById('myForm').web_url.focus()", 25); // refocus on it
                return false;
            } 
    		else {
                document.getElementById("urlerr").innerHTML ="";
            }
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    You are confusing two ways of addressing a form element.


    Code:
    <input type = "text" id = "web_url" onblur = "testit()">
    
    <script type = "text/javascript">
    
    function testit() {
    var val = document.getElementById("web_url").value;
    val = val.replace(/^\\s+|\\s+\$/g,""); // strip all leading and trailing spaces
    val = val.replace(/\\s{2,}/g," ");  // convert multiple spaces into a single space
    val = val.replace (/[^a-z0-9\s\-\.\:]/gi,"");  // strip the characters that cannot appear in a url
    val = val.replace (/^http:/i, "") // strip the http at the start of the string
    val = val.replace (/www\./i, "") // strip the first instance of www.
    document.getElementById("web_url").value = val; // write it back to the field
    
    }
    </script>
    So - did anyone dare tell George Stephenson, "It's not Rocket science"?
    Last edited by Philip M; 05-29-2012 at 06:35 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Philip, thank you.

    That is much cleaner.
    I'll look at it now to see what I was confusing.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

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