要验证并连接,请在表格中输入电话号码
To validate and connvert entered phone number in a form
我正在处理一个HTML表单,它有4个字段,如下所示
Name
Email
Phone Number
Message
电话号码字段应接受10位数字。当我单击消息字段时,它会更改/接受格式(xxx) xxx-xxxx
。
我已经为javascript编写了这样做的函数,但当我点击消息字段时,数字不会改变。代码低于
如果有人能帮我解决这个问题,那将是一个很大的帮助。提前感谢!
function PhoneValidation(phone) {
if (!(this.isNull)) {
var str = this.rawValue;
var regExp = /^'d{10}$/;
if (regExp.test(str)) {
this.rawValue = "(" + str.substr(0, 3) + ") " + str.substr(3, 3) + "-" + str.substr(6, 4);
} else {
regExp = /^[1-9]'d{2}'s'd{3}'s'd{4}$/;
if (regExp.test(str)) {
this.rawValue = "(" + str.substr(0, 3) + ") " + str.substr(4, 3) + "-" + str.substr(8, 4);
} else {
regExp = /^'([1-9]'d{2}')'s?'d{3}'-'d{4}$/;
if (!(regExp.test(str))) {
xfa.host.messageBox("Please enter the telephone number in the format '(999) 999-9999'.");
this.rawValue = null;
xfa.host.setFocus(this);
}
}
}
}
}
下面的HTML:
<form id="contact-form" class="contact-form" method="post" action="">
<div class="result"></div>
<input type="text" name="contact[name]" id="name" placeholder="Name *">
<input type="text" name="contact[email]" id="email" placeholder="E-mail *">
<input type="text" name="phone" id="phone" placeholder="Phone" onChange="PhoneValidation(this)" ;>
<textarea cols="5" rows="5" name="contact[message]" id="message" placeholder="Message *"></textarea>
<input type="submit" class="btn-dark" value="SEND">
</form>
在validate
函数this = window
或其他函数中,所以我不知道!this.isNull
实际会做什么。
你可以把它改成类似的东西
function PhoneValidation(phone) {
if(phone.value) {
// set up the phone.value here
}
}
// bind the change event as you did.
<input type="text" name="phone" id="phone" placeholder="Phone" onChange="PhoneValidation(this)";>
EDIT上面的代码只是想法,请注意,在您的案例中,在PhoneValidation内部是this = window
。你已经通过了手机,所以试着使用它,你可以在这里进行演示http://jsfiddle.net/7qjz2/.总结
window.PhoneValidation = function(phone)
// cause I don't know where you put this js, so let bind it to window.
接下来,在侧函数中,rawValue
未定义,因此请改用phone.value
如果您不能通过条件,请通过为您的消息分区设置html
document.getElementById("result").innerHTML = "Whatever you want"
仅此而已。希望得到帮助!
if (!(this.isNull)) {
显然,保持简短,比如:
if (this) {
相关文章:
- 如何将表格中的文本添加到使用按钮输入的文本区域
- 如何使用编辑按钮启用表格行中的所有输入
- 如何通过按Enter提交输入表格,但在按shift和Enter时不提交
- jQuery检查自定义Google电子表格页面上输入和下拉列表的更改
- 单击时将表格单元格从范围更改为输入
- 谷歌电子表格的 HTML 输入字段
- 表格报告-动态比较特定用户的输入日期和现有日期
- 在输入字段之前提取表格单元格中的文本
- 避免在按下“”时提交表格;输入“;按钮
- 在提交表格时IE8赢得了't将来自输入的属性以外的属性添加到请求中
- 按钮对链接对输入类型=“;提交”;在表格上
- 使 CSS 输入的行为类似于表格单元格
- 选择的输入不会在表格内对齐
- Google 表格自定义函数的多个数组输入,可将一个数组除以另一个数组
- jQuery从TD(HTML表格)内的输入字段中获取值
- 如何在每个表格行中附加表单及其文件输入
- 汇总表格输入,并随表格一起提交总计
- 从html表格输入单元格点击
- 如何为表格输入表格单元格
- html注册表格输入框包含描述