简体   繁体   English

WordPress上的验证表格

[英]Validation form on Wordpress

I have a custom form in wordpress, with radioboxes and custom fields. 我在wordpress中有一个自定义表单,带有单选框和自定义字段。 I want to validate it prior to sending, and I´ve implemented the following JavaScript code: 我想在发送之前对其进行验证,并且已经实现了以下JavaScript代码:

<script type="text/javascript">// <![CDATA[
function validar()
{
     if(document.form1.duracion.value=="")
    {
        alert("Falta seleccionar la duración del Vídeo.")
        return false
    }
     if(document.form1.idiomas.value=="")
    {
        alert("Falta seleccionar el número de idiomas")
        return false
    }
     if(document.form1.provincias.value=="")
    {
        alert("Falta seleccionar el tipo de Post-Producción")
        return false
    }
     if(document.form1.zonas.value=="")
    {
        alert("Faltan seleccionar las zonas de grabación")
        return false
    }
}
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2];     val=document.getElementById(args[i]);
  if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
      if (p<1 || p==(val.length-1)) errors+='- El '+nm+' tiene algo mal escrito.\n';
    } else if (test!='R') { num = parseFloat(val);
      if (isNaN(val)) errors+='- ¿El '+nm+' no debería ser un número?\n';
      if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
        min=test.substring(8,p); max=test.substring(p+1);
        if (num<min || max<num) errors+='- '+nm+' debería ser un número entre '+min+' y '+max+'.\n';
  } } } else if (test.charAt(0) == 'R') errors += '- Nos gustaría saber tu '+nm+'\n'; }
} if (errors) alert('¡UPS! ¡Nos faltan tus datos de contacto!:\n'+errors);
document.MM_returnValue = (errors == '');
} }
//-->
</script>

The problem is that I only can put this code in the post body, not in the head. 问题是我只能将此代码放在发布正文中,而不能放在头部。 Then, I have the form button: 然后,我有表单按钮:

<input id="button" onclick="MM_validateForm('nombre','','R','empresa','','R','telefono','','RisNum','mail','','RisEmail');return document.MM_returnValue" type="submit" name="button" value="Obtener Presupuesto Instantáneo" />

The problem is, that it does not work. 问题是,它不起作用。 I´m not very confident on coding, so any help will be very useful. 我对编码不是很有信心,因此任何帮助将非常有用。 Also, If somebody has the abilities to make it work (both with JS, Jquery or anything), I´ll pay for the service. 另外,如果有人有能力使其工作(无论是与JS,Jquery还是其他工具一起使用),我都会为该服务付费。

Thanks. 谢谢。

Xabier 沙比尔

If you are not against JQuery you can use this plugin . 如果您不反对JQuery,则可以使用此插件 Its quite enough for 90% of validation tasks 它足以完成90%的验证任务

我相信使用此插件远离编码会容易得多

To have real time validation before form submission you could use eg 要在提交表单之前进行实时验证,可以使用例如

$('#myInputField').on('keyup', function(){})

Here is an example in a jsfiddle where I have put real time validation on a field. 这是jsfiddle中的一个示例,其中我对字段进行了实时验证。 Just and example I have asked that the username should be a length of 8 characters and applied validation accordingly. Just and example,我要求用户名长度为8个字符,并相应地应用验证。 Using this method you can validate your input fields before even pressing the button. 使用此方法,您甚至可以在按按钮之前验证输入字段。

http://jsfiddle.net/y7bet/ http://jsfiddle.net/y7bet/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM