PDA

View Full Version : removing characters from a text boxbefore submitting


lprais
10-11-2002, 12:59 AM
I would like to use javascript to strip out “?” if the user includes them in their search.

For example, if the user types in the search textbox “Where is the Cat?” in order for my search engine to work correctly I need to submit to the engine “Where is the Cat” notice the “?” can not be included. I am using verity and it treats “?” like “*” or wildcard.

Pseudo code:

If form.txt includes ? then
Remove.

Not sure how I would write this in javascript.

If anyone knows of a site that includes something similer to this please let me know

Feel free to ask me anything that is unclear

Any help would be appreciated.

Thank you in advance
Lance

ConfusedOfLife
10-11-2002, 01:20 AM
Well! It's very easy to make!

function Remove(field, value)
{
arr = field.value.split(value);
newVal = "";
for (i=0; i<arr.length; i++)
newVal += arr[i];
field.value = newVal;
}



Now if you wana use it in a form called "myForm" with a text field called "myText", searching for "?", you would write :

Remove(document.myForm.myText, "?");


I hope that it works.

whammy
10-11-2002, 02:02 AM
Hmm... I think he should use a regular expression.

mystring = mystring.replace(/\?/g,'')

should work fine for that. :D

ConfusedOfLife
10-11-2002, 02:32 AM
Yeah, you're right! ( using regualr expressions of course! ), sorry that I forgot your motto!

lprais
10-11-2002, 09:44 PM
I am trying to use the following code to stript out "?" from anywhere in a query string. It needs to find and replace "?" with "".

It appears to be working on ie 5.5 but not netscape or 6.0. The other problem is for example I enter
"test?" the output is "test"
"Test?" the output is "Test?"
"TEST?" the output is "TEST"

For somereason it does not work with mixed case. Am I way off or close?

Thanks in advance for your help
Lance

<script LANGUAGE="Javascript">

function CheckFields()
{
document.The_Form.QueryText.value = document.The_Form.QueryText.value.replace(/\?/g,'');
}
</script>

It is not working could some please teel me what I am doing wrong?

whammy
10-11-2002, 10:01 PM
How are you calling the function? That must be the problem.

Try:

<form>
<input type="text" onchange="this.value = this.value.replace(/\?/g,'')" />
</form>

lprais
10-11-2002, 10:50 PM
Thank you for you help, As you can see below I added the code to the input box and you will also notice how I call the function CheckFields(this validates that there is something in the textbox). I am getting an error. I looked online to see the see the proper syntax but counld not find the answer.

The error is a follows:

this.value = this.value.replace(/\?/g,
.....................................^

I noticed that there is an uneven number of ", oculd that be the problem?
<tr>
<td colspan="2"><font face="Arial, Helvetica, sans-serif" size="3">

<input type="text" name="QueryText" size="20" style="text-align: Left; width: 280" tabindex="1" maxlength="500" onchange="this.value = this.value.replace(/\?/g,'')" />
<font face="Arial, Helvetica, sans-serif" size="2">
<input type="submit" name="submitButton" value="Search" onClick="return(CheckFields())">
</td>
</tr>

Thanks for your help

lprais
10-11-2002, 11:31 PM
I have one more issuse that is strange and I have never come across anything like it.

I was able to get it to strip the "?" from the textbox.

Here is the issue for example:
on ie 6.0-- works onnetscape 6.0 but that is all I tested
If I enter "Testing?" and tab out or use the mouse to click submit it works correctly.

If I type "Testing?" and simply hit return it doesnot strip the "?"

Any Ideas how I can overcome this issue.

Thanks
Lance

whammy
10-11-2002, 11:38 PM
Try this, this should fix the submit problem:


<script type="text/javascript">
<!--
function CheckFields(f){
f.QueryText.value = f.QueryText.value.replace(/\?/g,'');
return true;
}
// -->
</script>


<form ...your other form tag stuff... onsubmit="return CheckFields(this)">

... stuff here

<input type="submit" name="submitButton" value="Search" />

.. stuff

</form>

adios
10-12-2002, 02:26 AM
<html>
<head>
<title>untitled</title>
</head>
<body>
<form action="javascript:alert(document.forms[0].QueryText.value)" method="post"
onsubmit="QueryText.value=QueryText.value.replace(/\?/g, '')">
<input type="text" name="QueryText">
<input type="submit" value="Search">
</form>
</body>
</html>