Replace characters in text field before form submission?

I’ll start by saying I know virtually nothing about Javascript, beyond a broad idea of where it goes and what it does.

I have a small problem in terms of getting a form submitted to my action page (which, if relevant, is programmed in Coldfusion). The text field may contain certain characters that I need to replace before they hit the action page. For example, the user may enter something like ‘Save £5.00’. I therefore need to change the ‘£’ to ‘£’ - but I need to do this before the form is submitted.

My gut feeling is this is a job for Javascript - can anyone enlighten me please?

I’d use PHP for this, it’s much faster and server side, so people can’t mess with that anymore:

<?php
    $field = $_POST['NameOfInput'];
    $field = str_replace('&#163;', '&pound;', $field);
?>

Thanks, but as mentioned it needs to be done client-side and before it hits the action page. I can do a replace with Coldfusion, but something strange is happening with the way the character is being transmitted between the form and the action page - hence the need to change it client-side.

Then you should look into that, instead of validating data client-side a visitor or user can enter. This could be a potential security risk if this situation occurs with other characters like quotes.

On client side you can use String.replace method.
Your Javascript code will be like


var s = document.NameOfForm.NameOFInput.value;
s = s.replace( /£/gi, '&pound;' );
//....

But the others are also right, you should better do it on a server-side.

Thanks f0kin, I appreciate that server-side is a better method, but in this instance the form isn’t public anyway, so a hack isn’t as much of an issue (and I’ve already spent several days trying to figure out what’s going wrong).

I’ve tried the code you give, however I’m struggling to get it to work. Not getting any error messages or anything else, but I suspect my extremely limited JS knowledge is the problem. This is what I have at the moment;

<script language="javascript">
function stringReplace(addcode) {
var s = document.addcode.getsyou.value;
s = s.replace( /£/gi, '&pound;' );
}
</script>

<form method="post" action="a_addcode.cfm" name="addcode">
<input type="text" name="code" />
<input type="text" name="getsyou" onblur= "stringReplace(this.form)" />
<input type="text" name="notes" />
<input name="submit" type="submit" value="Add" />
</form>

<script language="javascript">
function stringReplace(addcode) {
var s = document.addcode.getsyou.value;
[B]document.addcode.getsyou.value = [/B]s.replace( /£/gi, '&pound;' );
}
</script>

mey be so…? (i haven’t checked)

Nope, doesn’t seem to have made any difference (using FF on a Mac, if that’s relevant). Thanks for your input though, much appreciated.

I don’t think it is about FF or Mac. £ - is a char not from standard ascii table.
Ensure that you have UTF-8 encoding, and everything must work fine (I’ve checked :)).

btw, doing this action on blur is not elegant, you should do it “onsubmit” (in form attributes)