Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    removing unwanted characters from a passed value and passing to input field

    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:
    Code:
    text2.value=text1.value.replace(/\$/g,"");
    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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:
    Code:
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by nucleusnet View Post
    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.

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •