Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-29-2012, 04:16 PM   PM User | #1
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
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
bazz is offline   Reply With Quote
Old 05-29-2012, 04:28 PM   PM User | #2
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
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
bazz is offline   Reply With Quote
Old 05-29-2012, 06:33 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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"?
__________________

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.

Last edited by Philip M; 05-29-2012 at 06:35 PM..
Philip M is online now   Reply With Quote
Old 05-29-2012, 09:10 PM   PM User | #4
bazz
Master Coder

 
Join Date: Apr 2003
Location: in my house
Posts: 5,211
Thanks: 39
Thanked 201 Times in 197 Posts
bazz will become famous soon enoughbazz will become famous soon enough
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
bazz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:08 PM.


Advertisement
Log in to turn off these ads.