如何将焦点添加到验证错误?

我有一个验证文件,但我不知道在哪里添加焦点,以便在每次出错时,焦点都集中在发生错误的字段上.也许有人会对如何做到这一点有更好的想法.以下是我的代码:

选择 | 换行 | 行号
  1. <code>
  2. <script>
  3. // get the div element for the error messages
  4. var errorElem = document.getElementById("error");
  5.  
  6. // define some variables to store the values of the form elements
  7. var Name 
  8. var Street  
  9. var City 
  10. var countrySelect 
  11. var PostalZip 
  12. var DayPhone  
  13. var Email
  14. var shipName 
  15. var shipStreet  
  16. var shipCity 
  17. var shipcountrySelect 
  18. var shipPostalZip 
  19. var shipDayPhone  
  20. var shipEmail 
  21. var pName
  22. var pType
  23. var pExpiry
  24.  
  25. // define a function to do the validation
  26. function validate()
  27. {
  28. Name = nameAlpha();
  29. Street = document.bookform.billStreet.value;
  30. City = document.bookform.billStreet.value;
  31. CountrySelect = countryBlank();
  32. PostalZip = postalAlphaNum();
  33. DayPhone = phoneNum();
  34. Email = validateEmail();
  35. shipName = nameAlphaShip();
  36. shipStreet = document.bookform.shipStreet.value;
  37. shipCity = document.bookform.shipStreet.value;
  38. shipCountrySelect = countryBlankShip();
  39. shipPostalZip = postalAlphaNumShip();
  40. shipDayPhone = phoneNumShip();
  41. pName = payNameAlpha();
  42. pType = paymentBlank();
  43. pNum = payNum();
  44. pExpiry = expiryBlank();
  45.  
  46.    var isValid = false;
  47.  
  48.    if (Name  && Street !== ""  && City !== ""  && 
  49.       CountrySelect && PostalZip  && DayPhone  && Email && shipName  
  50.       && shipStreet !== ""  && shipCity !== ""  && 
  51.       shipCountrySelect  && shipPostalZip  &&
  52.        shipDayPhone && pName && pType && pNum && pExpiry)
  53.    {
  54.       isValid = true;
  55.    }
  56.  
  57.    if (!isValid) 
  58.    {
  59.       handleError();
  60.    }
  61.    else  // clear any error messages
  62.    {
  63.       errorElem.innerHTML = "";
  64.    }
  65.  
  66.    /* The return value is very important in form validation.
  67.    If you return a true value, the form will be submitted; 
  68.    a false value will prevent the form from being submitted. */ 
  69.    return isValid;
  70. }
  71. //-------------------------------------------------------------------------------
  72.  
  73. // ensure that characters entered are alpha only
  74. function nameAlpha()
  75. {
  76.    // get the text element
  77.    var textfield = document.bookform.billName;   
  78.    // get the value of the name entered by the user
  79.    var namev = textfield.value;
  80.    // get the div tag for the error message
  81.    var errorElem = document.getElementById("error");
  82.  
  83.    var pattern = /[^a-zA-Z ]/;
  84.    if ((namev === "" || namev === "undefined") || 
  85.       pattern.test(namev))
  86.    {
  87.  
  88.       return false;  // prevent the form from submitting
  89.    }
  90.  
  91.    // everything ok, so allow the form to be submitted
  92.    errorElem.innerHTML = "";  // clear the error message
  93.    return true;
  94. }
  95.  
  96. // ensure that characters entered are alpha only. SHIPPING
  97. function nameAlphaShip()
  98. {
  99.    // get the text element
  100.    var textfield = document.bookform.shipName;   
  101.    // get the value of the name entered by the user
  102.    var namev = textfield.value;
  103.    // get the div tag for the error message
  104.    var errorElem = document.getElementById("error");
  105.  
  106.    var pattern = /[^a-zA-Z ]/;
  107.    if ((namev === "" || namev === "undefined") || 
  108.       pattern.test(namev))
  109.    {
  110.  
  111.       return false;  // prevent the form from submitting
  112.    }
  113.  
  114.    // everything ok, so allow the form to be submitted
  115.    errorElem.innerHTML = "";  // clear the error message
  116.    return true;
  117. }
  118.  
  119. //for country drop down
  120. function countryBlank()
  121. {
  122. var textfield = document.bookform.billCountrySelect;
  123. var countryv = textfield.value
  124. var errorElem = document.getElementById("error");
  125.  
  126. if (countryv.length < 1)
  127. {
  128.       return false;  // prevent the form from submitting
  129.    }
  130.  
  131.    // everything ok, so allow the form to be submitted
  132.    errorElem.innerHTML = "";  // clear the error message
  133.    return true;
  134. }
  135.  
  136. //for country drop down. SHIPPING
  137. function countryBlankShip()
  138. {
  139. var textfield = document.bookform.shipCountrySelect;
  140. var countryv = textfield.value
  141. var errorElem = document.getElementById("error");
  142.  
  143. if (countryv.length < 1)
  144. {
  145.       return false;  // prevent the form from submitting
  146.    }
  147.  
  148.    // everything ok, so allow the form to be submitted
  149.    errorElem.innerHTML = "";  // clear the error message
  150.    return true;
  151. }
  152.  
  153. // ensure that characters entered are alphanumerical only
  154. function postalAlphaNum()
  155. {
  156.    // get the text element
  157.    var textfield = document.bookform.billPostalZip;   
  158.    // get the value of the name entered by the user
  159.    var postal = textfield.value;
  160.    // get the div tag for the error message
  161.    var errorElem = document.getElementById("error");
  162.  
  163.    var pattern = /[^a-zA-Z0-9]/;
  164.    if ((postal === "" || postal === "undefined") || 
  165.       pattern.test(postal))
  166.    {
  167.  
  168.       return false;  // prevent the form from submitting
  169.    }
  170.  
  171.    // everything ok, so allow the form to be submitted
  172.    errorElem.innerHTML = "";  // clear the error message
  173.    return true;
  174. }
  175.  
  176. // ensure that characters entered are alphanumerical only. SHIPPING
  177. function postalAlphaNumShip()
  178. {
  179.    // get the text element
  180.    var textfield = document.bookform.shipPostalZip;   
  181.    // get the value of the name entered by the user
  182.    var postal = textfield.value;
  183.    // get the div tag for the error message
  184.    var errorElem = document.getElementById("error");
  185.  
  186.    var pattern = /[^a-zA-Z0-9]/;
  187.    if ((postal === "" || postal === "undefined") || 
  188.       pattern.test(postal))
  189.    {
  190.  
  191.       return false;  // prevent the form from submitting
  192.    }
  193.  
  194.    // everything ok, so allow the form to be submitted
  195.    errorElem.innerHTML = "";  // clear the error message
  196.    return true;
  197. }
  198.  
  199. // ensure that characters entered are numerical only for phone number
  200. function phoneNum()
  201. {
  202.    // get the text element1
  203.    var textfield1 = document.bookform.billDayPhone1;   
  204.    // get the value of the name entered by the user
  205.    var phone1 = textfield1.value;
  206.  
  207.    var textfield2 = document.bookform.billDayPhone2;   
  208.    var phone2 = textfield2.value;
  209.  
  210.    var textfield3 = document.bookform.billDayPhone3;   
  211.    var phone3 = textfield3.value;
  212.  
  213.    // get the div tag for the error message
  214.    var errorElem = document.getElementById("error");
  215.  
  216.    var pattern = /[^0-9 ]/;
  217. if (
  218.      (phone1 === "" || phone1 === "undefined") ||
  219.      (phone2 === "" || phone2 === "undefined") || 
  220.      (phone3 === "" || phone3 === "undefined") ||
  221.      (pattern.test(phone1 && phone2 && phone3)))
  222.    {
  223.  
  224.       return false;  // prevent the form from submitting
  225.    }
  226.  
  227.    // everything ok, so allow the form to be submitted
  228.    errorElem.innerHTML = "";  // clear the error message
  229.    return true;
  230. }
  231.  
  232. // ensure that characters entered are numerical only for phone number. SHIPPING
  233. function phoneNumShip()
  234. {
  235.    // get the text element1
  236.    var textfield1 = document.bookform.shipDayPhone1;   
  237.    // get the value of the name entered by the user
  238.    var phone1 = textfield1.value;
  239.  
  240.    var textfield2 = document.bookform.shipDayPhone2;   
  241.    var phone2 = textfield2.value;
  242.  
  243.    var textfield3 = document.bookform.shipDayPhone3;   
  244.    var phone3 = textfield3.value;
  245.  
  246.    // get the div tag for the error message
  247.    var errorElem = document.getElementById("error");
  248.  
  249.    var pattern = /[^0-9 ]/;
  250.    if ((phone1 === "" || phone1 === "undefined" && phone2 === "" || 
  251.       phone2 === "undefined" || phone3 === "" && phone3 === "undefined") ||
  252.       pattern.test(phone1 && phone2 && phone3))
  253.    {
  254.  
  255.       return false;  // prevent the form from submitting
  256.    }
  257.  
  258.    // everything ok, so allow the form to be submitted
  259.    errorElem.innerHTML = "";  // clear the error message
  260.    return true;
  261. }
  262.  
  263. function validateEmail()
  264.    var formElem = document.bookform
  265.    // get the text element
  266.    var textfield = document.bookform.billEmail;   
  267.    // get the value of the name entered by the user
  268.    var emailv = textfield.value;
  269.    // get the div tag for the error message
  270.    var errorElem = document.getElementById("error");
  271.  
  272. var pattern = /^[a-zA-Z0-9\- ]+\@[a-zA-Z0-9 \-\.]+\.([a-zA-Z]{2,3})$/;
  273.    if ((emailv === "" || emailv === "undefined") || 
  274.       !pattern.test(emailv))
  275.    {
  276.       return false;  // prevent the form from submitting
  277.    }
  278.  
  279.    // everything ok, so allow the form to be submitted
  280.    errorElem.innerHTML = "";  // clear the error message
  281.    return true;
  282. }
  283.  
  284.  
  285. //Payment Validation-------------------------------------------------
  286.  
  287. function payNameAlpha()
  288. {
  289.    // get the text element
  290.    var textfield = document.bookform.payName;   
  291.    // get the value of the name entered by the user
  292.    var payName = textfield.value;
  293.    // get the div tag for the error message
  294.    var errorElem = document.getElementById("error");
  295.  
  296.    var pattern = /[^a-zA-Z ]/;
  297.    if ((payName === "" || payName === "undefined") || 
  298.       pattern.test(payName))
  299.    {
  300.  
  301.       return false;  // prevent the form from submitting
  302.    }
  303.  
  304.    // everything ok, so allow the form to be submitted
  305.    errorElem.innerHTML = "";  // clear the error message
  306.    return true;
  307. }
  308.  
  309. //for credit card drop down
  310. function paymentBlank()
  311. {
  312. var textfield = document.bookform.paymentType;
  313. var pay = textfield.value
  314. var errorElem = document.getElementById("error");
  315.  
  316. if (pay.length < 1)
  317. {
  318.       return false;  // prevent the form from submitting
  319.    }
  320.  
  321.    // everything ok, so allow the form to be submitted
  322.    errorElem.innerHTML = "";  // clear the error message
  323.    return true;
  324. }
  325.  
  326. function payNum()
  327. {
  328.    // get the text element
  329.    var textfield = document.bookform.payNum;   
  330.    // get the value of the phone number entered by the user
  331.    var payNumv = textfield.value;
  332.    // get the div tag for the error message
  333.    var errorElem = document.getElementById("error");
  334.  
  335.    var pattern = /[0-9]/;
  336.    if ((payNumv === "" || payNumv === "undefined") || !pattern.test(payNumv))
  337.    {
  338.  
  339.       return false;  // prevent the form from submitting
  340.    }
  341.  
  342.    // everything ok, so allow the form to be submitted
  343.    errorElem.innerHTML = "";  // clear the error message
  344.    return true;
  345. }
  346.  
  347.  
  348. //for expiry drop downs
  349. function expiryBlank()
  350. {
  351. var textfield1 = document.bookform.expiryMonth;
  352. var month = textfield1.value
  353.  
  354. var textfield2 = document.bookform.expiryYear;
  355. var year = textfield2.value
  356.  
  357. var errorElem = document.getElementById("error");
  358.  
  359. if (month.length < 1 && year.length < 1)
  360. {
  361.  
  362.       return false;  // prevent the form from submitting
  363.    }
  364.  
  365.    // everything ok, so allow the form to be submitted
  366.    errorElem.innerHTML = "";  // clear the error message
  367.    return true;
  368. }
  369.  
  370. //------------------------------------------------------------------
  371. function handleError()
  372. {
  373.    if (!Name) 
  374.       errorElem.innerHTML = "Please enter your name. Letters and spaces only.";
  375.    else if (Street === "") 
  376.       errorElem.innerHTML = "Please provide a street addess.";
  377.    else if (City === "")
  378.       errorElem.innerHTML = "Please provide a city.";
  379.    else if (!CountrySelect)
  380.       errorElem.innerHTML = "Please select your country.";
  381.    else if (!PostalZip) 
  382.       errorElem.innerHTML = "Please provide a postal/zip code. Letters and numbers only";
  383.    else if (!DayPhone) 
  384.       errorElem.innerHTML = "Please provide a phone number. Numbers only.";
  385.    else if (!Email) 
  386.       errorElem.innerHTML = "Please provide an email. Must have an '@' and '.'";
  387.    else if (!shipName) 
  388.       errorElem.innerHTML = "Please enter your shipping name. Letters and spaces only.";
  389.    else if (shipStreet === "") 
  390.       errorElem.innerHTML = "Please provide a shipping street addess.";
  391.    else if (shipCity === "")
  392.       errorElem.innerHTML = "Please provide a shipping city.";
  393.    else if (!shipCountrySelect)
  394.       errorElem.innerHTML = "Please select your shipping country.";
  395.    else if (!shipPostalZip) 
  396.       errorElem.innerHTML = "Please provide a shipping postal/zip code. Letters and numbers only.";
  397.    else if (!shipDayPhone) 
  398.       errorElem.innerHTML = "Please provide a shipping phone number. Numbers only.";
  399.    else if (!pName) 
  400.       errorElem.innerHTML = "Please enter your credit card name. Letters and spaces only.";
  401.    else if (!pType) 
  402.       errorElem.innerHTML = "Please select your credit card.";
  403.    else if (!pNum) 
  404.       errorElem.innerHTML = "Please provide a credit card number. Numbers only.";
  405.    else if (!pExpiry) 
  406.       errorElem.innerHTML = "Please provide an expiry date.";
  407. }
  408. </script>
  409. </code>
  410.  

请帮帮忙!:)
谢谢

# 回答1


在有问题的元素上使用Focus(),例如

选择 | 换行 | 行号
  1. field.focus();
# 回答2


是的我试过了.例如,我尝试将它添加到每个验证函数的返回FALSE上方,它在某种程度上是有效的,但它从底部开始.因此,我的顶部错误消息将会显示(如果我正在验证一个空表单),但焦点将转到底部字段.为什么会这样呢?我还可以在哪里添加它?
# 回答3


将这些行添加到每个if/Else部分的handleError函数中(您需要添加括号以允许2个语句).

标签: Javascript

添加新评论