PDA

View Full Version : replacing the %20


sarah
03-26-2003, 04:46 PM
Hi All,

How do I replace the %20 in a text field with a space? I have a text field in which a User enters their full address, when I <script>document.write(address);</script> I get "1%20Street%20Name... etc. Is here some way I can replace the %20 with a space?

Thanks
Sarah

beetle
03-26-2003, 04:53 PM
document.write(unescape(address));

or

document.write(address.replace( /%20/g, " " ));

sarah
03-26-2003, 07:24 PM
Thanks Beetle,

there's a bug coming your way (its a blonde female beetle) - lol!

sarah
03-26-2003, 09:13 PM
Hi anyone,

I used beetle's solution to replace the %20 using unescape(fieldname), but one particular field has the plus sign (+) and document.write displays:

Help+In+Need

I tried using the unescape but it didn't work, so I tried:

<script>document.write(desc.replace( /+/g, " "));</script>
but I got "an unexpected quantifier error".

Anyways round this prob?

Sarah

beetle
03-26-2003, 09:22 PM
Yup, the + in a pattern is a special metacharacter that is a quantifier meaning "one or more". So, for a pattern to match the literal character '+', you need to escape it with a backslash

desc.replace( /\+/g, " "));

sarah
03-26-2003, 09:31 PM
Great stuff Beetle!

Thanks a Million!

Sarah

P.s Does anyone know how I can validate an email address. I searched on the forum and found summat called regexp. I tried to use it but it wouldn't validate the email address (Probably cos I dont know the syntax and was jus guessing). If any one can send me to a link where an example is shown.

Here's wat i tried:

if (email.value=="") {
alert("Please enter your email address"); }
else {
if (!('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',email.value)) {
alert("Please enter a valid email address"); }
}

but it jus doesn't like it, and doesn't validate the email address.

beetle
03-26-2003, 09:39 PM
Your close. Regular Expressions use patterns to perform operations on strings (just like the replacing we did above)

One method of a regular expression is test, which we'd apply here.

if ( !/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/.test( email.value ) )
{
// doesn't match
}
else
{
// does match
}

sarah
03-26-2003, 09:48 PM
ok,

i tried this:

if (!'/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/'.test ( email.value)) {
alert("Please enter a valid email address"); }

and it didnt work, so i tried:

if (!/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/.test ( email.value)) {
alert("Please enter a valid email address"); }

and that didnt work either.

Cant understand y?

beetle
03-26-2003, 09:52 PM
Do you get any sort of error message? Normally, you can't and shouldn't address a form field directly by it's name or ID.

instead of email.value something more like document.forms[0].email.value or some other reference...

sarah
03-28-2003, 10:55 AM
i dont get any error messages, but the validation doesnt occur and the form submits itself.

so i tried the following:

if (email.value=="") {
alert("Please enter your email address"); }
else {
if (!(/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/.test (document.addressform.email.value ))) {
alert("Please enter a valid email address"); }
}

and this now works fine. The enter valid address alert works fine. In my function I also use the following line of code:

if (surname.value!="" && forename.value!="" && address1.value!="" && city.value!="" && postcode.value!="" && email.value!="") {
location.href="form2.htm?surname=" + surname.value; }

this line of code checks that all the required fields are populated and then goes onto the nextpage with the relevant information passed on. How do I also check to see if the email is valid then carry on validating else don't do anything?

I tried the following:

error="FALSE";

if (email.value=="") {
alert("Please enter your email address"); }
else {
if (!(/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/.test (document.addressform.email.value ))) {
error="TRUE";
alert("Please enter a valid email address"); }
}
}

then in the following line:

if (surname.value!="" && forename.value!="" && address1.value!="" && city.value!="" && postcode.value!="" && email.value!="") {
location.href="form2.htm?surname=" + surname.value; }

i changed it to:

if (surname.value!="" && forename.value!="" && address1.value!="" && city.value!="" && postcode.value!="" && email.value!="" && error="false") {
location.href="form2.htm?surname=" + surname.value; }

I keep getting the following error message "error is undefined ".

What am I doing wrong?

Thanx again.
Sarah

glenngv
03-28-2003, 11:28 AM
there are couple of things you did wrong:

1. as beetle said, you should not reference the fields like email.value. looking at your regexp code, the form name is addressform, so you should reference the fields like:

document.addressform.email.value

you can save the form reference to a variable so it's easier to type and more efficient:

var f = document.addressform;

so reference all your fields like:

f.email.value
f.surname.value
f.forename.value
...

2. javascript has a Boolean datatype, true and false (without quotes, all lowercase)
so you should use false or true in assigning values to the error variable.

3. You should use == instead of = in comparing values:

&& email.value!="" && error==false)

4. You should escape the username in the URL, so that special characters that may contain in the username will be escaped (the % thing):

location.href="form2.htm?surname=" + escape(f.surname.value);

This will lessen the errors but without seeing the whole code and how you call it, I can't totally solve your problem. I just pointed out the syntax problems and not the logic part

sarah
03-28-2003, 12:26 PM
Hi Glen,

thanx for the tips. i'll give a try.

Sarah