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