Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URLencode and Submit a form

Status
Not open for further replies.

Krus1972

Programmer
Mar 18, 2004
145
US
Hello,

I have the following JavaScript code. The purpose of the code is to check the content of a search form to insure that special characters are not being submitted. If there is an attempt to submit special characters, a warning message appears and the script will not submit the form. If there are no special characters, the script will submit the form.

The only special character that is allow is a comma. The script works good except for one thing.

If you enter a phrase such as "test, this" the string in the browser will read "test,%20this".
The script is not properly URLencoding the comma.

Does anyone know how to make the string getting properly encoded for the comma and any other special character we may allow, in the future, within the form input?

Thanks for your time.


function validSearch()
{

// Check For a Valid Search
if(document.searchform.theadd.value==''){
alert("Invalid Entry")
return false;
}
if(document.searchform.theadd.value==' '){
alert("Invalid Entry")
return false;
}

if(document.searchform.theadd.value==' '){
alert("Invalid Entry")
return false;
}
{
var iChars = "!@#$%^&*()+=-[]\';/{}|\":<>?";
for (var i = 0; i < document.forms["searchform"].theadd.value.length; i++) {
if (iChars.indexOf(document.forms['searchform'].theadd.value.charAt(i)) != -1)
{
alert("Invalid Entry - Enter only letters and numbers. Special characters are not allowed.")
return false;
}
}
{
var iChars = "!@#$%^&*()+=-[]\';/{}|\":<>?";
for (var i = 0; i < document.forms["searchform"].location.value.length; i++) {
if (iChars.indexOf(document.forms['searchform'].location.value.charAt(i)) != -1)
{
alert("Invalid Entry - Enter only letters and numbers. Special characters are not allowed.")
return false;
}
}




}

// alert("Valid Search")

document.searchform.action ="/";
document.searchform.submit();
}
}
 
The script is not properly URLencoding the comma.

commas are allowed in URLs, so do not require encoding, spaces are not so are encoded.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top