View Full Version : removing unwanted characters from a passed value and passing to input field

Oct 20th, 2009, 03:09 AM
My java skill are rudimentary at best.

I have an email which has some values attached to a link in the email that get passed to an online form on a website when clicked. Problem is the value is a price and the string includes a "$" which I need to remove. I've read through many examples and everyone seems to have an opinion (surprise). One suggests something like this:

While someone else says its better to remove what you don't want. I am also scratching my head over should I place the value into a hidden field, scrub it then copy that into the proper field? and I imagine the best way would be to run it with an "onload" command instead or a event handler like a keystroke since the field is not typed? Roundtable discussion would be appreciated to help me solve this simple problem and help expand my knowledge of java... thanks.

Old Pedant
Oct 20th, 2009, 03:26 AM
It depends on whether you want to fix up all the human idiocies or only some of them.

And it also depends on whether you need the cleaned up versions to be visible to self-same humans or not.

There's no reason you couldn't do the replacement "in place" if you wanted to submit the <form> to some server side code that will send off the email:

function cleanUp( form )
// first, zap all characters except digits, period, and minus sign
var price = form.price.value.replace(/[^0123456789\.\-]/g, "" );
// then convert what's left to a number...
price = parseFloat( price );
// what do you do if what's left is *NOT* a number??
// something like this???
if ( isNaN( price ) ) { alert("Price is not a valid number."); return false; }

// so put the formatted number, all cleaned up, back into the form:
form.price.value = price.toFixed(2);

... do other stuff to other field ...

return true; // don't forget this

and then you have something like:

<form action="sendMail.xxx" method="post" onsubmit="return cleanUp(this);">

But of course there are as many answers as there are coders. You have to decide the path you want to take.

Philip M
Oct 20th, 2009, 08:34 AM
My java skill are rudimentary at best.......
Roundtable discussion would be appreciated to help me solve this simple problem and help expand my knowledge of java... thanks.

This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

text2.value=text1.value.replace(/\$/g,""); replaces the $ sign by nothing, that is removes it.

Oct 20th, 2009, 09:24 AM
Yes, I know its a javascript forum...sorry for the shorthand. Let me try and explain better what I am trying to accomplish. An email is generated and sent to a customer, in the email is a link that is clicked. This link has some input values added so when the page is navigated to, data is automatically propagated to several input field so the user does not have to fill those in. The problem lays is one of the values is for the "amount" field and the data includes a "$" in the price. This form is for processing a payment and the only data a user has to add is their credit card info. The gateway cannot process the "amount" field because of the "$" hence the need to remove it. The form I use is generated by the system. I can change HTML but the form processing is done with a script held by the system that I do not have access to - I can't use any "onclick" or "onsubmit" type functions because the system's internal scripts are already using those handlers. So my challenge is to change the the value in the amount field by removing the "$" and perform the script "on-the-fly." I thought of maybe passing the amount value to a hidden field, strip it, and then create the proper field with the properly formatted value using a "document.write"...so confused and lost...I am not sure if that would work because I do not know the order of items that are rendered in a browser.

Old Pedant
Oct 20th, 2009, 10:42 PM
And this page that is a link in the email is an HTML page???

That's mildly unusual and not very secure. I would think it would be some kind of server-side page (PHP, ASP, JSP, whatever).

But anyway...

Okay, so just perform the script at <body onload=...> time.

Yes, you can do that. Even if there already is a an onload specified.

if ( navigator.appName == "Microsoft Internet Explorer" )
attachEvent('onload', yourFixupFunction, false);
} else {
addEventListener('load', yourFixupFunction, false);