Javascript 防止html表单根据输入值提交
我有一个带有引导和谷歌应用程序脚本的表单。我需要为字段配置特定的验证。如果id为“estado”的输入字段的值为1,则不应提交表单。输入字段的值由google应用程序脚本函数生成,并在输入字段“inputid”更改时更新 这是我的代码:Javascript 防止html表单根据输入值提交,javascript,forms,google-apps-script,submit,form-submit,Javascript,Forms,Google Apps Script,Submit,Form Submit,我有一个带有引导和谷歌应用程序脚本的表单。我需要为字段配置特定的验证。如果id为“estado”的输入字段的值为1,则不应提交表单。输入字段的值由google应用程序脚本函数生成,并在输入字段“inputid”更改时更新 这是我的代码: <div class="form-row"> <div class="form-group col-md-6"> <label for="inputid&qu
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputid">Identificador de viaje</label>
<input type="text" class="form-control" id="inputid" onchange="onchangeestado()" required>
<div class="invalid-feedback">
No ha ingresado un identificador de viaje.
</div>
</div>
<div class="form-group col-md-6">
<label for="estado">Estado</label>
<input type="text" class="form-control" id="estado" required disabled>
<div class="invalid-feedback">
El viaje señalado ya se encuentra cerrado.
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="kminicial">Kilometraje Final</label>
<input type="number" class="form-control" id="kmfinal" required>
<div class="invalid-feedback">
No ha ingresado el kilometraje inicial del vehículo.
</div>
</div>
</div>
<button class="btn btn-primary" type="button" data-toggle="modal" data-target="#modal" onclick="verifyError()">Enviar datos</button>
<div id="modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Envío de Formulario</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>¿Desea enviar el registro?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
<button class="btn btn-outline-primary" id ="enviar" >Registrar Salida</button>
</div>
</div>
</div>
</div>
<script>
var arrayOfValues;
function verifyError() {
var estado = document.getElementById("estado");
var inputid = document.getElementById("inputid");
if(estado == 1) {
inputid.classList.add("is-invalid");
}
else{
document.getElementById("enviar").addEventListener("click",afterButtonClicked);
}
}
function afterButtonClicked(){
if(validate()){
var cuenta = document.getElementById("cuenta");
var inputid = document.getElementById("inputid");
var estado = document.getElementById("estado");
var kmfinal = document.getElementById("kmfinal");
var rowDataarrive = {cuenta: cuenta.value,inputid:inputid.value,
estado: estado.value,kmfinal:kmfinal.value,
};
google.script.run.addNewRowarrive(rowDataarrive);
$('#modal').modal('hide')
$('#successnotification').toast('show')
setTimeout(function(){location.reload()}, 6000);
}
else{
$('#modal').modal('hide')
$('#errornotification').toast('show')
}
}
function validate(){
var fieldsToValidate = document.querySelectorAll("#userform input, #userform select");
Array.prototype.forEach.call(fieldsToValidate, function(el){
if(el.checkValidity()){
el.classList.remove("is-invalid");
}
else{
el.classList.add("is-invalid");
}
});
return Array.prototype.every.call(fieldsToValidate, function(el){
return el.checkValidity();
});
}
function getId(){
var idCode = document.getElementById("inputid").value;
google.script.run.withSuccessHandler(updateIdcode).getId(idCode);
}
function updateIdcode(estadolist){
document.getElementById("estado").value = estadolist;
}
google.script.url.getLocation(function(location) {
document.getElementById("inputid").value = location.parameters.inputid[0];
getId();
});
document.getElementById("inputid").addEventListener("input",getId);
document.getElementById("loading").remove();
</script>
维亚杰酒店
没有人能认出维亚杰。
埃斯塔多
厄尔维亚杰·塞纳拉多是塞拉多的一个城市。
公里决赛
没有什么特别的地方。
环境达托斯
公式化环境
&时代;
你是说什么
塞拉
注册官萨利达
var阵列价值;
函数verifyError(){
var estado=document.getElementById(“estado”);
var inputid=document.getElementById(“inputid”);
如果(estado==1){
inputid.classList.add(“无效”);
}
否则{
document.getElementById(“enviar”).addEventListener(“单击”,点击后按钮);
}
}
函数afterButtonClicked(){
if(validate()){
var cuenta=document.getElementById(“cuenta”);
var inputid=document.getElementById(“inputid”);
var estado=document.getElementById(“estado”);
var kmfinal=document.getElementById(“kmfinal”);
var rowDataArrival={cuenta:cuenta.value,inputid:inputid.value,
estado:estado.value,kmfinal:kmfinal.value,
};
google.script.run.addNewRowArrival(RowDataArrival);
$('#model')。model('hide'))
$('successnotification')。toast('show')
setTimeout(函数(){location.reload()},6000);
}
否则{
$('#model')。model('hide'))
$('#errornotification').toast('show'))
}
}
函数验证(){
var fieldsToValidate=document.queryselectoral(#用户表单输入,#用户表单选择”);
Array.prototype.forEach.call(fieldsToValidate,function(el){
if(el.checkValidity()){
el.classList.remove(“无效”);
}
否则{
el.classList.add(“无效”);
}
});
返回Array.prototype.every.call(fieldsToValidate,function(el){
返回el.checkValidity();
});
}
函数getId(){
var idCode=document.getElementById(“inputid”).value;
google.script.run.withSuccessHandler(updateIdcode).getId(idCode);
}
函数updateIdcode(estadolist){
document.getElementById(“estado”).value=estadList;
}
google.script.url.getLocation(函数(位置){
document.getElementById(“inputid”).value=location.parameters.inputid[0];
getId();
});
document.getElementById(“inputid”).addEventListener(“input”,getId);
document.getElementById(“加载”).remove();
当我提交表单时,表单以相同的方式发送,当值等于“1”时,在我的电子表格上注册值。
你能帮我解决这个问题吗?
你好 我不确定表单验证需要的数字,显然不是数字1。因此,您可以尝试以下方法:
<input type="text" class="form-control" id="estado" onkeypress='validate(event)' required disabled>
不确定这是否适用于您,您可能必须在“var regex”中使用数字参数才能使其在您的页面上工作。
这还可以防止用户输入错误的数据,或试图复制/粘贴到字段中。我有两个参数,1和0。当输入值等于0时,如果该值等于1,则提交。不提交表单。我看不出在该函数上传递参数的位置。对不起,我对Javascript非常陌生。你能解释一下吗?啊,我明白了,在正则表达式中你没有包含数字1 ajajja对不起。。。我现在将尝试…我尝试了代码,但现在表单在任何情况下都不会发送…@LuisGermánOrregoCampano请分享您的发现。很高兴您找到了解决方案。请与社区分享。
function validate(evt) {
var theEvent = evt || window.event;
// Handle paste
if (theEvent.type === 'paste') {
key = event.clipboardData.getData('text/plain');
} else {
// Handle key press
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
}
var regex = /[2-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}