简体   繁体   English

从表单文本输入字段中删除最后四个字符

[英]Remove last four characters from form text input field

I have a form for data collection and one text input field is for tracking numbers.我有一个数据收集表单,一个文本输入字段用于跟踪号码。 I use a barcode scanner to scan shipping labels as an easy way to capture the tracking number.我使用条码扫描器来扫描运输标签,以此轻松获取跟踪号。 Then I paste the number into that field.然后我将数字粘贴到该字段中。 However, this one particular type of shipping label, from FedEx, adds the form number to the end of the tracking number.但是,这种来自 FedEx 的特殊类型的运输标签会将表单编号添加到跟踪编号的末尾。

Is there a way to use JavaScript or JQuery to check the input, right after it is pasted, and remove the last four characters if they are '0663'?有没有办法在粘贴后立即使用 JavaScript 或 JQuery 检查输入,并删除最后四个字符(如果它们是“0663”)? Can someone provide a sample if this is possible?如果可能的话,有人可以提供样品吗?

Here's a working solution.这是一个有效的解决方案。 Just run the code snippet below.只需运行下面的代码片段。

Tested in Chrome.在 Chrome 中测试。 May require some of the commented code to be uncommented in other browsers.可能需要在其他浏览器中取消注释某些已注释的代码。

 const input = document.getElementById('fred'); const removeEnd = (value) => { console.log('value', value); if (value.match('0663$')) { const newValue = value.slice(0, -4); input.value = newValue; } } input.onkeyup = (evt) => { removeEnd(evt.target.value); } input.onpaste = (evt) => { // might be needed // removeEnd(evt.target.value); } input.oninput = (evt) => { // might be needed // removeEnd(evt.target.value); }
 <input id="fred">

Here:这里:

 let code = '123412341234123412340663'; console.log(code.substring(0,code.length-4));

The following demo uses:以下演示使用:

  • HTMLFormControlsCollection to access the <form> , <input> , and <output> tags. HTMLFormControlsCollection以访问<form><input><output>标签。
  • oninput event to call the callback function: lastFour() . oninput事件调用回调函数: lastFour() input event happens when the registered <input> tag gets any data entered by the user. input事件在注册的<input>标签获取用户输入的任何数据时发生。
  • substring() and slice() to manipulate the string value substring()slice()来操作字符串值

 // Reference the <form> var shp = document.forms[0].elements; // Reference the <input> var trk = shp.trackingNumber; // Assign event handler for input event trk.oninput = lastFour; // Callback function function lastFour(e) { /* Get the value of the tag that the input event occurred on (ie <input>) */ var num = e.target.value; /* if the last 4 chars equals '0663'... [?] ...extract the chars from num at start(0) to the fourth to the last(-4 excluded)...[:] ...otherwise leave num alone */ var mod = (num.substring(num.length - 4) === '0663') ? num.slice(0, -4) : num; // Set the value of <output> to the new value mod shp.labelScan.value = mod; }
 <form id='shipping'> <label for='trackingNumber'>Tracking Number: <input id='trackingNumber' type='number' min='100000000000' max='9999999999999999'> 12 to 16 digits</label><br> <output id='labelScan'></output> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM