943,152 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 655
  • PHP RSS
Jan 15th, 2010
0

form email validation does not work

Expand Post »
Hi
I use the following form validation and it works all fine, except my email input does not get validated.

Input type = email
field name = emailto

this is the code:
PHP Syntax (Toggle Plain Text)
  1. function formCheck(formobj){
  2. // Enter name of mandatory fields
  3. var fieldRequired = Array("emailto","from", "emailfrom","zigi");
  4. // Enter field description to appear in the dialog box
  5. var fieldDescription = Array("Friends E-mail","Friends Name", "Your E-mail","Your Name");
  6. // dialog message
  7. var alertMsg = "These fields cannot be left blank:\n";
  8.  
  9. var l_Msg = alertMsg.length;
  10.  
  11. for (var i = 0; i < fieldRequired.length; i++){
  12. var obj = formobj.elements[fieldRequired[i]];
  13. if (obj){
  14. switch(obj.type){
  15. case "select-one":
  16. if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
  17. alertMsg += " - " + fieldDescription[i] + "\n";
  18. }
  19. break;
  20. case "select-multiple":
  21. if (obj.selectedIndex == -1){
  22. alertMsg += " - " + fieldDescription[i] + "\n";
  23. }
  24. break;
  25. case "text":
  26. case "textarea":
  27. if (obj.value == "" || obj.value == null){
  28. alertMsg += " - " + fieldDescription[i] + "\n";
  29. }
  30. break;
  31. case "email":
  32. if (obj.value == "" || obj.value == null){
  33. alertMsg += " - " + fieldDescription[i] + "\n";
  34. }
  35. var strValue = emailto.obj.value;
  36. var strFilter = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i;
  37. if (!strFilter.test(strValue))
  38. alertMsg = "wrong or incomplete email" + fieldDescription[i] + "\n";
  39. }
  40. break;
  41. default:
  42. }
  43. if (obj.type == undefined){
  44. var blnchecked = false;
  45. for (var j = 0; j < obj.length; j++){
  46. if (obj[j].checked){
  47. blnchecked = true;
  48. }
  49. }
  50. if (!blnchecked){
  51. alertMsg += " - " + fieldDescription[i] + "\n";
  52. }
  53. }
  54. }
  55. }
  56. if (alertMsg.length == l_Msg){
  57. return true;
  58. }else{
  59. alert(alertMsg);
  60. return false;
  61. }
  62. }

What am I missing in the email validation, please?

Thank a lot
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeroge is offline Offline
13 posts
since Dec 2009
Jan 15th, 2010
0
Re: form email validation does not work
it might not be the way you want to go, but if it were me, I'd be using jquery and a jquery plugin to do that.

here's one that works perfectly
http://www.reynoldsftw.com/2009/03/l...n-with-jquery/
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Jan 15th, 2010
0
Re: form email validation does not work
Tks.
But I have no experience in using jquery for a form ... plus, this would need to redo the entire form from scratch and I worked 3 days on that form ... to get it where it is

Learning jquery would take me too long right now. But worth considering for the future, that's for sure
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeroge is offline Offline
13 posts
since Dec 2009
Jan 15th, 2010
0
Re: form email validation does not work
you wouldnt have to redo the form. Just


1) add the jquery.js and plugin files to the server
2) include the jquery.js and plugin files in your html
3) add like 3 lines of code.


that's the beauty of jquery and why it's slogan is: "WRITE LESS DO MORE"

it's designed to SAVE you time
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Jan 15th, 2010
0
Re: form email validation does not work
This is the form page (.php)
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. include('config.php');
  3. ?>
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  5.  
  6. <html>
  7. <head>
  8. <!-- Start Colorbox -->
  9. <link type="text/css" media="screen" rel="stylesheet" href="colorbox.css" />
  10. <script type="text/javascript" src="lightview/jquery.min.js"></script>
  11. <script type="text/javascript" src="lightview/jquery.colorbox.js"></script>
  12. <script type="text/javascript">
  13. $(document).ready(function(){
  14. $(".iframe").colorbox({width:"50%", height:"85%", iframe:true});
  15.  
  16. //Example of preserving a JavaScript event for inline calls.
  17. $("#click").click(function(){
  18. $('#click').css({"background-color":"#f00", "color":"#000", "cursor":"inherit"}).text("Contact Zeroge Business Directory");
  19. return false;
  20. });
  21. });
  22. </script>
  23. <!-- End Colorbox -->
  24.  
  25. <title><? echo $title; ?></title>
  26.  
  27. <script language="JavaScript">
  28.  
  29.  
  30. var highlightcolor="lightyellow"
  31.  
  32. var ns6=document.getElementById&&!document.all
  33. var previous=''
  34. var eventobj
  35.  
  36. //Regular expression to highlight only form elements
  37. var intended=/INPUT|TEXTAREA|SELECT|OPTION/
  38.  
  39. //Function to check whether element clicked is form element
  40. function checkel(which){
  41. if (which.style&&intended.test(which.tagName)){
  42. if (ns6&&eventobj.nodeType==3)
  43. eventobj=eventobj.parentNode.parentNode
  44. return true
  45. }
  46. else
  47. return false
  48. }
  49.  
  50. //Function to highlight form element
  51. function highlight(e){
  52. eventobj=ns6? e.target : event.srcElement
  53. if (previous!=''){
  54. if (checkel(previous))
  55. previous.style.backgroundColor=''
  56. previous=eventobj
  57. if (checkel(eventobj))
  58. eventobj.style.backgroundColor=highlightcolor
  59. }
  60. else{
  61. if (checkel(eventobj))
  62. eventobj.style.backgroundColor=highlightcolor
  63. previous=eventobj
  64. }
  65. }
  66.  
  67. </script>
  68. <style type="text/css">
  69. html {
  70. padding:0px;
  71. margin:0px;
  72. }
  73.  
  74. body {
  75. font-family: Georgia, Serif;
  76. font-weight: bold;
  77. padding: 0px 20px;
  78. margin: 0px;
  79. }
  80.  
  81. #menu {
  82. font-family: Tahoma, Serif;
  83. font-weight: normal;
  84. font-size:11px;
  85. color:#414A5A;
  86. position: absolute;
  87. width: 380px;
  88. left: 20px;
  89. padding: 20px;
  90. margin:0px;
  91. background: #F5F5F5;
  92. border: 1px dashed #000000;
  93. }
  94.  
  95. #content {
  96. font-family: Arial;
  97. font-weight: normal;
  98. font-size:10px;
  99. color:#414A5A;
  100. width:380px;
  101. margin-top: 95px;
  102. left: 20px;
  103. background: #F5F5F5;
  104. padding: 0px 10px 20px 30px;
  105. border: 1px dashed #000000;
  106. }
  107.  
  108. h1 {
  109. font-size:large;
  110. padding-top: 10px;
  111. }
  112. </style>
  113. </head>
  114.  
  115. <body>
  116.  
  117.  
  118. <br />
  119. <div id="menu">
  120. <tr>Recommend this page to your friend</tr>
  121. <tr></tr>
  122. </div>
  123.  
  124. <div id="content">
  125.  
  126. <h1><? echo $title; ?></h1><hr><br>
  127.  
  128. <script language="JavaScript">
  129.  
  130. function formCheck(formobj){
  131. // Enter name of mandatory fields
  132. var fieldRequired = Array("to","from", "say","zigi");
  133. // Enter field description to appear in the dialog box
  134. var fieldDescription = Array("Friends E-mail","Friends Name", "Your E-mail","Your Name");
  135. // dialog message
  136. var alertMsg = "These fields cannot be left blank:\n";
  137.  
  138. var l_Msg = alertMsg.length;
  139.  
  140. for (var i = 0; i < fieldRequired.length; i++){
  141. var obj = formobj.elements[fieldRequired[i]];
  142. if (obj){
  143. switch(obj.type){
  144. case "select-one":
  145. if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
  146. alertMsg += " - " + fieldDescription[i] + "\n";
  147. }
  148. break;
  149. case "select-multiple":
  150. if (obj.selectedIndex == -1){
  151. alertMsg += " - " + fieldDescription[i] + "\n";
  152. }
  153. break;
  154. case "text":
  155. case "textarea":
  156. if (obj.value == "" || obj.value == null){
  157. alertMsg += " - " + fieldDescription[i] + "\n";
  158. }
  159. break;
  160. default:
  161. }
  162. if (obj.type == undefined){
  163. var blnchecked = false;
  164. for (var j = 0; j < obj.length; j++){
  165. if (obj[j].checked){
  166. blnchecked = true;
  167. }
  168. }
  169. if (!blnchecked){
  170. alertMsg += " - " + fieldDescription[i] + "\n";
  171. }
  172. }
  173. }
  174. }
  175. if (alertMsg.length == l_Msg){
  176. return true;
  177. }else{
  178. alert(alertMsg);
  179. return false;
  180. }
  181. }
  182.  
  183. </script>
  184.  
  185.  
  186.  
  187. <p><form name="formcheck" method="post" action="http://chinabiz21.com/support/refer/mail.php" onsubmit="return formCheck(this) ///onKeyUp="highlight(event)" onClick="highlight(event);">
  188.  
  189. <strong>Friends E-mail</strong><br/>
  190. <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="to" size="35"><br>
  191.  
  192. <strong>Friends Name</strong><br/>
  193. <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="from" size="35"><br>
  194. <strong>Your E-mail</strong><br/>
  195. <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="say" size="35"><br>
  196. <strong>Your Name</strong><br/>
  197. <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="zigi" size="35"><br>
  198. <strong>Your Message</strong><br/>
  199. <input type=text style="font-size: 13px; font-family: tahoma,arial; font-weight: bold; color: #336699; BORDER: #000000 1px line ; BACKGROUND-COLOR: #F8F8F8" name="msg" size="35"><br>
  200. <p></p>
  201. <input TYPE="submit" NAME="Request" VALUE="Submit This Form">
  202. <input TYPE="reset" NAME="Clear" VALUE="Clear Form and Start Over">
  203.  
  204. </form></p>
  205. <p>
  206. </p>
  207. </div>
  208.  
  209. </body>
  210. </html>
the mail.php page has two other includes and the whole form opens in a light box ... you can view the form sample in action here, simply click on the left rollover (refer a friend) and the form will open in the lightbox

and this is the mail.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. include('config.php');
  3. include('mail.body.php');
  4. $time = date ("h:i A");
  5. $date = date ("l, F jS, Y");
  6. $urlh = getenv(HTTP_HOST);
  7. $url = "http://$urlh";
  8. $IP = $_SERVER['REMOTE_ADDR'];
  9. $to = $_POST['to'];
  10. $subject = $_POST['subject'];
  11. $say = $_POST['say'];
  12. $from = $_POST['from'];
  13. $msg = $_POST['msg'];
  14. $zigi = $_POST['zigi'];
  15. $body ='
  16. <html>
  17. <head>
  18. <style>
  19. <!--
  20. body, P.msoNormal, LI.msoNormal
  21. {
  22.  
  23. background-position: top;
  24. background-color: #ffffff;
  25. margin-left: 2em;
  26. margin-top: 0em;
  27. margin-bottom: 4em;
  28. font-family: "verdana";
  29. font-size: 9pt;
  30. font-weight: ;
  31. color: "000000";
  32.  
  33.  
  34.  
  35. }
  36. -->
  37. </style>
  38. </head>
  39. </body>
  40. ';
  41. $headers = 'MIME-Version: 1.0' . "\r\n";
  42. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  43. $headers .= 'From: Chinabiz21.com Admin <me@myurl.com>' . "\r\n";
  44. $bodys .= ("
  45. $zigi found something on $site which you might find
  46. interesting. Click on this <a href=$url>link</a> to view the information, or click <a href=mailto:$say>here</a> to reply $zigi on this message.<br><br>$zigi also left this private message for you:<hr>
  47. ");
  48. $bodys .= "<b><font color=#2B60DE>$msg</font><b><br><hr>";
  49. $bodys .= "<li>The message was left on: $date, $time <br>";
  50. $bodys .= "<li>Recommended Site URL: $url";
  51. $subject .= " $from - $zigi recommends $site";
  52. $body = $body . $bodys . $message;
  53. mail($to, $subject ,$body, $headers);
  54. ?>
  55. <?php
  56. include('config.php');
  57. $urlh = getenv(HTTP_HOST);
  58. $url = "http://$urlh";
  59. $email;
  60. $date = date ("l, F jS, Y");
  61. $time = date ("h:i A");
  62. $subject = "Info";
  63. $body = $_POST['body'];
  64. $bodys = "$title\nMessage:\n$msg\nThis mesage sent to $form was submitted on\n $date at\n $time.\n From IP Address.$IP \n From E Address\n$say\nTo E Address\n $to. \nUrl.\n $url ";
  65. $IP = $_SERVER['REMOTE_ADDR'];
  66. $body = $body . $bodys;
  67. mail($email, $subject, $body, "From: $email");
  68. ?>
  69. <html>
  70. <head>
  71. <style>
  72. <!--
  73. body, P.msoNormal, LI.msoNormal
  74. {
  75.  
  76. background-position: top;
  77. background-color: #ffffff;
  78. margin-left: 2em;
  79. margin-top: 0em;
  80. margin-bottom: 4em;
  81. font-family: "verdana";
  82. font-size: 9pt;
  83. font-weight: bold;
  84. color: "#151B8D";
  85.  
  86.  
  87.  
  88. }
  89. -->
  90. </style>
  91. </head>
  92. </body>
  93.  
  94. <script type="text/javascript">
  95. <!--
  96. var count = 4; // currently set to 5 seconds - change as necessary
  97.  
  98. function counter() // this function must be called in the html document as follows: <body onload="counter()">
  99. {
  100. count--;
  101.  
  102. if(count == 0)
  103. {
  104. window.location = "/"; // enter the location that you wish to redirect visitors to
  105. }
  106.  
  107. setTimeout("counter()", 1000); // timeout function invokes and loops the function every second.
  108. }
  109. //-->
  110. </script>
  111. </head>
  112. <body onLoad="counter()"> <!-- function must be invoked when the page loads //-->
  113. <center><strong><p>Thank you! Your email has been sent to your friend. Redirection....</p></strong></center>
  114. </body>
  115. </html>

SO REALLY, I WOULD NOT HAVE A CLUE WHERE AND HOW TO PLACE THE LIVE-VALIDATION SO IN ORDER TO KEEP THE FORM FUNCTIONAL ...

cCheers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeroge is offline Offline
13 posts
since Dec 2009
Jan 15th, 2010
0
Re: form email validation does not work
well best of luck to ya then.
Reputation Points: 34
Solved Threads: 51
Posting Whiz
kireol is offline Offline
305 posts
since Mar 2008
Jan 15th, 2010
0
Re: form email validation does not work
Sure then, ya absolutely
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeroge is offline Offline
13 posts
since Dec 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Includes in header?
Next Thread in PHP Forum Timeline: Full-Featured PHP Upload Class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC