SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use PHP for this, it's much faster and server side, so people can't mess with that anymore:

    PHP Code:
    <?php
        $field 
    $_POST['NameOfInput'];
        
    $field str_replace('£''&pound;'$field);
    ?>
    FOR SALE: 1 set of morals, never used, will sell cheap

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Evangelist hexburner's Avatar
    Join Date
    Jan 2007
    Location
    Belgium
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    FOR SALE: 1 set of morals, never used, will sell cheap

  5. #5
    SitePoint Member f0kin's Avatar
    Join Date
    Jul 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On client side you can use String.replace method.
    Your Javascript code will be like
    Code:
    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.

  6. #6
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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;

    Code:
    <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>

  7. #7
    SitePoint Member f0kin's Avatar
    Join Date
    Jul 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script language="javascript">
    function stringReplace(addcode) {
    var s = document.addcode.getsyou.value;
    document.addcode.getsyou.value = s.replace( //gi, '&pound;' );
    }
    </script>
    mey be so...? (i haven't checked)

  8. #8
    SitePoint Member
    Join Date
    Feb 2007
    Location
    UK
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Member f0kin's Avatar
    Join Date
    Jul 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think it is about FF or Mac. &#163; - is a char not from standard ascii table.
    Ensure that you have UTF-8 encoding, and everything must work fine (I've checked ).

  10. #10
    SitePoint Member f0kin's Avatar
    Join Date
    Jul 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw, doing this action on blur is not elegant, you should do it "onsubmit" (in form attributes)


Bookmarks

Posting Permissions

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