2014-03-19 23 views
0

我在wordpress中有一個自定義窗體,包含放射性盒和自定義字段。我想先對其進行驗證以發送和從來就實施了以下JavaScript代碼:Wordpress上的驗證表格

<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> 

的問題是,我只能把這個代碼在後機身,而不是在頭部。 然後,我有表單按鈕:

<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" /> 

的問題是,它不工作。 我對編碼不太自信,所以任何幫助都會非常有用。另外,如果有人有能力使其工作(無論是JS,JQuery或任何東西),我會支付服務。

謝謝。

Xabier的

回答

0

如果你不反對的JQuery,你可以使用這個plugin。其相當足夠的驗證任務90%

0

保持距離有你可以使用例如表單提交之前實時驗證

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

這裏是一個jsfiddle的例子,我已經把實時驗證放在一個字段上。只是和示例我已經要求用戶名應該是8個字符的長度並相應地進行驗證。使用這種方法,您甚至可以在按下按鈕之前驗證輸入字段。

http://jsfiddle.net/y7bet/