提交表单时JS未定义错误
JS undefined error when form is submited
我有这样一个表单:
<form onsubmit="return validarIDC()">
<div class="labelBox">
<div>
<label>Destinatario:</label>
<select name="destinatario">
<option value="hombre">Sr.</option>
<option value="mujer">Sra.</option>
</select>
</div>
<div>
<label>Apellido y<br>nombre:</label>
<input type="text" id="nombre"> *
</div>
<div id="ubicarCampo">
<label>Razón Social:</label>
<input type="text" name="razon">
</div>
<div>
<label>Email:</label>
<input type="email" name="email"> *
</div>
<div>
<label>Teléfono:</label>
<input type="text" name="telefono"> *
</div>
<div>
<label>Celular:</label>
<input type="text" name="celular">
</div>
<div>
<label>Via de Contacto:</label>
<select name="via">
<option value="opc1">E-mail</option>
<option value="opc2">Telefono</option>
<option value="opc3">Correo postal</option>
</select>
</div>
<div>
<label>Comentarios:</label>
<textarea max="300"></textarea>
</div>
<input name="submit" type="submit" value="Enviar">
</div>
</form>
<script type="text/javascript" src="script.js"></script>
和我已经做了一个函数来验证所有的数据从输入,它的工作完美,除了一个错误,更多的细节后的代码,这是JS:
function validarIDC() {
var errores = [];
var er = /^['w]+$/;
if (document.contactForm.nombre.value == "" || document.contactForm.nombre.value == null) {
errores.push("El nombre es obligatorio.");
}
else if (!er.test(document.contactForm.nombre.value)) {
errores.push("El nombre contiene caracteres no validos o Espacios.");
}
if (document.contactForm.telefono.value == "") {
errores.push("Debe ingresar un telefono")
}
else if (isNaN(document.contactForm.telefono.value)) {
errores.push("El campo telefono contiene caracteres no validos.");
}
if (document.contactForm.email.value == "") {
errores.push("Debe especificar una dirección de Email.");
}
if (document.contactForm.celular.value !== "" && isNaN(document.contactForm.celular.value)) {
errores.push("El campo celular contiene caracteres no validos.");
}
if (errores.length > 0) {
msg = alert("Error/es: 'n");
for (var i = 0; i < errores.length; i++) {
msg += errores[i] + "'n";
}
alert(msg);
return false;
}
document.contactForm.submit.disable = true;
alert("Los datos han sido enviados exitosamente!");
return true;
}
所以当我提交时,它弹出警报(msg),但令人惊讶的是,当任何条件为真时,我得到"未定义"粘贴到错误的一边。控制台日志什么也没说,我不知道我做错了什么。有谁能帮我一下吗?
您需要初始化变量,因为alert()
不返回任何内容,msg
是undefined
。
msg = "Error/es: 'n";
不是msg = alert("Error/es: 'n");
可以使用.join()
var msg = "Error/es: 'n" + errores.join("'n");
你应该为你的表单指定一个名称和id…
下面是一个工作代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form onsubmit="return validarIDC()" name="contactForm" id="contactForm">
<div class="labelBox">
<div>
<label>Destinatario:</label>
<select name="destinatario">
<option value="hombre">Sr.</option>
<option value="mujer">Sra.</option>
</select>
</div>
<div>
<label>Apellido y<br>nombre:</label>
<input type="text" id="nombre" name="nombre"> *
</div>
<div id="ubicarCampo">
<label>Razón Social:</label>
<input type="text" name="razon">
</div>
<div>
<label>Email:</label>
<input type="email" name="email"> *
</div>
<div>
<label>Teléfono:</label>
<input type="text" name="telefono"> *
</div>
<div>
<label>Celular:</label>
<input type="text" name="celular">
</div>
<div>
<label>Via de Contacto:</label>
<select name="via">
<option value="opc1">E-mail</option>
<option value="opc2">Telefono</option>
<option value="opc3">Correo postal</option>
</select>
</div>
<div>
<label>Comentarios:</label>
<textarea max="300"></textarea>
</div>
<input name="submit" type="submit" value="Enviar">
</div>
</form>
<script type="text/javascript">
function validarIDC() {
var errores = [];
var er = /^['w]+$/;
if (document.contactForm.nombre.value == "" || document.contactForm.nombre.value == null) {
errores.push("El nombre es obligatorio.");
}
else if (!er.test(document.contactForm.nombre.value)) {
errores.push("El nombre contiene caracteres no validos o Espacios.");
}
if (document.contactForm.telefono.value == "") {
errores.push("Debe ingresar un telefono")
}
else if (isNaN(document.contactForm.telefono.value)) {
errores.push("El campo telefono contiene caracteres no validos.");
}
if (document.contactForm.email.value == "") {
errores.push("Debe especificar una dirección de Email.");
}
if (document.contactForm.celular.value !== "" && isNaN(document.contactForm.celular.value)) {
errores.push("El campo celular contiene caracteres no validos.");
}
if (errores.length > 0) {
msg = alert("Error/es: 'n");
for (var i = 0; i < errores.length; i++) {
msg += errores[i] + "'n";
}
alert(msg);
return false;
}
document.contactForm.submit.disable = true;
alert("Los datos han sido enviados exitosamente!");
return true;
}
</script>
</body>
</html>
相关文章:
- chrome.storage.sync未定义错误
- 对象为null或IE9中存在未定义错误
- 已定义变量出现未定义错误
- 'define' 在 RequireJS 和 Webapp Yo 生成器上未定义错误
- onreadystatechange未定义错误
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 如何修复JSlint报告的“foo未定义”错误
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 主干无法读取属性'属性'主干视图中的未定义错误
- 如何克服javascript中的未定义错误
- require未定义错误
- 单击链接时出现函数未定义错误
- 函数为未定义错误
- 主干未定义错误
- 使用Javascript打印HTML:未定义错误
- 避免“无法读取”属性'获取'的未定义错误
- 未捕获类型错误的coffescription:无法读取属性'offsetWidth'的未定义错误
- ajax上get函数内部的未定义错误
- Javascript变量未定义错误
- AngularJS指令作用域未解析(“属性名称未定义”错误)