简体   繁体   English

使用Javascript将一个文本字段拆分为两个文本,进行验证,然后再次合并

[英]Splitting one text-field into two with Javascript, validate it, then merge it back again

A client is asking that a textfield containing phone-number data be split into two -- one for country code, the other for the rest of the number. 一位客户要求将包含电话号码数据的文本字段分为两部分-一个用于国家/地区代码,另一个用于其余的号码。

Alas, this field is pretty hard-coded into the system and all data collected thus far has been as one consolidated field (and thus saved as one column in the database). field,此字段很难被硬编码到系统中,并且到目前为止收集的所有数据都作为一个合并字段(并因此保存为数据库中的一列)。

I'm thus thinking the best answer might be to do the following: 因此,我认为最好的答案可能是执行以下操作:

  1. Using Javascript, replace the single text field with two text fields. 使用Javascript将单个文本字段替换为两个文本字段。
  2. These then become merged back into the original text field when the user clicks the submit button. 然后,当用户单击“提交”按钮时,这些内容将合并回原始文本字段中。
  3. Bonus marks if there's a way to separate that field back into two again when it's read from the database (Ie, when an administrator views the entry). 奖励标记是否存在一种方法,当从数据库中读取该字段时(即,当管理员查看该条目时)将该字段再次分为两部分。 Note that the data format must be consistent -- I don't want to mix the existing string data with, say, a bunch of JSON strings. 请注意,数据格式必须一致-我不想将现有的字符串数据与一堆JSON字符串混合使用。

Is this the best way to go about this? 这是解决此问题的最佳方法吗? Are there any foreseeable problems (beyond the user not having JavaScript enabled) with this approach? 这种方法是否存在任何可预见的问题(除了用户未启用JavaScript之外)? Is there a jQuery plugin that's designed to do stuff like this? 是否有一个jQuery插件专门用于执行此类操作?

I also need to validate it as a valid phone number at some point, but I can figure that out myself. 我还需要在某个时候将其验证为有效的电话号码,但我自己可以弄清楚。

Rather than splitting the text field into two, you may use the masked input JQuery plug-in 您可以使用屏蔽的输入JQuery插件,而不是将文本字段分为两部分。

http://digitalbush.com/projects/masked-input-plugin/ http://digitalbush.com/projects/masked-input-plugin/

$(document).ready(function(){
   $("#phone").mask("(999)999999");                
});

Sample JS : http://jsfiddle.net/vGeMV/3/ JS范例: http : //jsfiddle.net/vGeMV/3/

Edit: For point 3 : If you want to really split the number into two and display it, you may split it from the right (usually 7 digits) rather than left so that even if country code is present or not, you will get the correct split. 编辑:对于第3点:如果您想将数字真正分成两部分并显示出来,则可以从右侧(通常是7位数字)而不是左侧进行拆分,以便即使存在国家代码也可以正确分割。

If you dont want to use a plugin you can always check out this snippet I made on jsFiddle 如果您不想使用插件,则可以随时查看我在jsFiddle上制作的此代码段

If you want to make sure it works in the JS just change the display:'none' to display:'block' 如果要确保它在JS中有效,只需将display:'none'更改为display:'block'

here is my JavaScript 这是我的JavaScript

$(function() {
    $('#phone').css({ display:'none' });
    $('#phone').after(') <input type="text" name="phoneMainNumber" id="phoneMainNumber" />').after('(<input type="text" value="555" name="phoneAreaCode" id="phoneAreaCode" style="width:30px;" />');
    bindChange();
});

function bindChange() {
    $('#phone').val('('+$('#phoneAreaCode').val()+') ');
    $('#phoneMainNumber, #phoneAreaCode').keyup(function() {
       $('#phone').val('('+$('#phoneAreaCode').val()+') '+$('#phoneMainNumber').val());    
    });   
}

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

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