我有一个像这样的表格
如何验证输入字段中是否有网址
<form onsubmit='return formValidation()'>
<p id="p4"></p>
<label>Address:</label>
<input type='text' id='addr' /><br />
<br/>
<input type="button" id="submit" value='Suubmit'/>
</form>
我的脚本是
function formValidation(){
var addr = document.getElementById('addr');
if(textAlphanumeric(addr, "* For Address please use numbers and letters *")){
return true;
}
return false;
}
function textAlphanumeric(inputtext, alertMsg){
var alphaExp = /^\d+\s[0-9]\s[a-z]+\s[A-Z]+/;
if(inputtext.value.match(alphaExp)){
return true;
}else{
document.getElementById('p4').innerText = alertMsg;
inputtext.focus();
return false;
}
}
为什么它不重播我任何内容
答案 0 :(得分:2)
如果您在提交提交表单时必须使用“按钮”标签。
function formValidation(){
var addr = document.getElementById('addr');
if(textAlphanumeric(addr, "* For Address please use numbers and letters *")){
return true;
}
return false;
}
function textAlphanumeric(inputtext, alertMsg){
var alphaExp = /^\d+\s[0-9]\s[a-z]+\s[A-Z]+/;
if(inputtext.value.match(alphaExp)){
document.getElementById('p4').innerText = "";
inputtext.focus();
return true;
}else{
document.getElementById('p4').innerText = alertMsg;
inputtext.focus();
return false;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form onsubmit='return formValidation();'>
<p id="p4"></p>
<label>Address:</label>
<input type='text' id='addr' /><br />
<br/>
<button id="submit" value='Submit'>Submit</button>
</form>