Jump to content

Get substring between characters - help


Recommended Posts

When one of my end users fills out a form then need to use the barcode scanner. An example of the text would be "%247463$1$0$4$20%". My script below is suppose to extract everything after  the first two characters up to the last character. It works but it is also showing the original along with it.

<script>

    function modify()
    {
        //Store the value in the field to trim.
        var trim = document.getElementsByName("InsertRecordBarcode")[0].value;
        //Take a substring of trim and store it back into new trim value.
        trim = trim.match("%2(.*)%");
        //Set the value of the weight field in the form to the new substring 
        document.getElementsByName("InsertRecordBarcode")[0].value = trim;
        //the new value in the field is what is submitted.
    }
    //When the form is submitted called method modify.
    document.getElementById("caspioform").onsubmit=modify;
</script>

 

My result showing is "%247463$1$0$4$20%,47463$1$0$4$20". What am I doing wrong as I want it to only show the 47463$1$0$4$20.

thank you.

Link to comment
Share on other sites

try to remove the value of the text field before assigning new value

    function modify()
    {
        //Store the value in the field to trim.
        var trim = document.getElementsByName("InsertRecordBarcode")[0].value;
        //Take a substring of trim and store it back into new trim value.
        trim = trim.match("%2(.*)%");
        //Set the value of the weight field in the form to the new substring 
document.getElementsByName("InsertRecordBarcode")[0].value = "";
        document.getElementsByName("InsertRecordBarcode")[0].value = trim;
        //the new value in the field is what is submitted.
    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...