473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to add focus to validation error?

I have a validation file and i do not know where to add the focus so that with each error it focuses on the field where the error occured. Maybe someone will have a better idea on how to do this. Here is my code:

Expand|Select|Wrap|Line Numbers
  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.  
Please help! :)

Thanks
Mar 10 '11 #1
3 2080
acoder
16,027 Expert Mod 8TB
Use focus() on the element in question, e.g.
Expand|Select|Wrap|Line Numbers
  1. field.focus();
Mar 14 '11 #2
yes i've tried that. For example, I've tried to add it to each validation function just above return false and it works in a way but it starts at the bottom. So my top error message will display(if im validating an empty form) but the focus goes to the bottom field. Why would that be? Where else can i add it?
Mar 14 '11 #3
acoder
16,027 Expert Mod 8TB
Add those lines to the handleError function in each if/else section (you will need to add brackets to allow 2 statements).
Mar 14 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: stuart dent via .NET 247 | last post by:
XML validation error. Help required If anyone can help me, thankyou, thankyou... When I run this code code I get this error: The data at the root level is invalid. Line 1, position 39. I...
2
by: Alan Silver | last post by:
Hello, I'm getting an odd validation error from VWD. As I understand it, an opening ASP.NET for tag is supposed to look like... <form runat="server"> with an optional ID attribute. VWD...
1
by: BillAtWork | last post by:
Hi, I'm trying to validate an XML document against an XSD schema and I receive the following error: ---------- MyCode.CreateValidRequest : System.Web.Services.Protocols.SoapException :...
1
by: BillAtWork | last post by:
Hi, I'm trying to validate an XML document against an XSD schema and I receive the following error: ---------- MyCode.CreateValidRequest : System.Web.Services.Protocols.SoapException :...
1
by: C.W.Holeman II | last post by:
I have an xmlns attribute that produces an XHTML validation error and I do not understand why it is considered an error.The file displays as expected on Firefox and IE7. ...
0
by: Nadeeka | last post by:
Hi, I wrote a Schema Validator in C#.NET. XmlTextReader R = new XmlTextReader(FileToParse); XmlValidatingReader V = new XmlValidatingReader(R); try ...
12
by: Vengo | last post by:
Hi, I have a doubt whether it is possible to give the form validation error msg to the tooltip which is used for field description. thanks in advance, cheers, vengo.
1
by: mimranp | last post by:
Hi all i m using visual developer 2008 i m using xhtml1.1 in that .i m reciving so much error when check in online input w3 standard(C# language code behind) <%@ Page Language="C#"...
2
by: ryan.paquette | last post by:
Hello' I have a field in which I require validation in a before update event. The validation code I have works fine, no problem there. However I would like to turn off the default validation...
1
by: Izzy123 | last post by:
i'm having a problem trying to validate my form for my assignment, the error is as follow: Line 103, Column 15: there is no attribute "name" <form name="checkForm"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.