...

View Full Version : filtering form input - going wrong



bazz
05-29-2012, 04:16 PM
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?



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

bazz
05-29-2012, 04:28 PM
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]



//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 ="";
}

Philip M
05-29-2012, 06:33 PM
You are confusing two ways of addressing a form element.



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

bazz
05-29-2012, 09:10 PM
Philip, thank you.

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

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum