如何使用JQuery插件inputmask获取不带括号的值

How to get value without brackets using JQuery plugin inputmask?

本文关键字:获取 何使用 JQuery 插件 inputmask      更新时间:2023-09-26

有以下代码:

$("#ce_clientphone").inputmask("+9(999)9999999")
...
console.log($('#ce_clientphone').unmask())

正如您所看到的,我试图从没有括号的"#ce_clientphone"中获取值。我该怎么做?我需要允许用户输入有效的电话,但要将其保存在数据库中,我需要删除括号。提前谢谢。

您可以使用正则表达式和replace来删除括号

"+9(999)9999999".replace(/[()]/g,'');
var str="+9(999)9999999";
alert(str.replace(/[()]/g,''));

DEMO

试试这个:

$('#ce_clientphone').unmask().replace(/[()]/g, '');

例如:

"+9(999)9999999".replace(/[()]/g, '');

退货:

"+99999999999"

那么,这是怎么回事呢?

我们使用正则表达式来替换括号:

/  // Start of regex
[  // Start of character group
() // match either of these characters in the group (So match `(` OR `)`)
]  // End of character group
/  // End of regex
g  // `Global` flag. This means the regex won't stop searching after the first replace