Guest User

Untitled

a guest
May 2nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. USING JAVA SCRIPT
  2. function formValidation()
  3. {
  4. var uid = document.registration.userid;
  5. var passid = document.registration.passid;
  6. var uname = document.registration.username;
  7. var uadd = document.registration.address;
  8. var ucountry = document.registration.country;
  9. var uzip = document.registration.zip;
  10. var uemail = document.registration.email;
  11. var umsex = document.registration.msex;
  12. var ufsex = document.registration.fsex; if(userid_validation(uid,5,12))
  13. {
  14. if(passid_validation(passid,7,12))
  15. {
  16. if(allLetter(uname))
  17. {
  18. if(alphanumeric(uadd))
  19. {
  20. if(countryselect(ucountry))
  21. {
  22. if(allnumeric(uzip))
  23. {
  24. if(ValidateEmail(uemail))
  25. {
  26. if(validsex(umsex,ufsex))
  27. {
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. return false;
  37.  
  38. } function userid_validation(uid,mx,my)
  39. {
  40. var uid_len = uid.value.length;
  41. if (uid_len == 0 || uid_len >= my || uid_len < mx)
  42. {
  43. alert("User Id should not be empty / length be between "+mx+" to "+my);
  44. uid.focus();
  45. return false;
  46. }
  47. return true;
  48. }
  49. function passid_validation(passid,mx,my)
  50. {
  51. var passid_len = passid.value.length;
  52. if (passid_len == 0 ||passid_len >= my || passid_len < mx)
  53. {
  54. alert("Password should not be empty / length be between "+mx+" to "+my);
  55. passid.focus();
  56. return false;
  57. }
  58. return true;
  59. }
  60. function allLetter(uname)
  61. {
  62. var letters = /^[A-Za-z]+$/;
  63. if(uname.value.match(letters))
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. alert('Username must have alphabet characters only');
  70. uname.focus();
  71. return false;
  72. }
  73. }
  74. function alphanumeric(uadd)
  75. {
  76. var letters = /^[0-9a-zA-Z]+$/;
  77. if(uadd.value.match(letters))
  78. {
  79. return true;
  80. }
  81. else
  82. {
  83. alert('User address must have alphanumeric characters only');
  84. uadd.focus();
  85. return false;
  86. }
  87. }
  88. function countryselect(ucountry)
  89. {
  90. if(ucountry.value == "Default")
  91. {
  92. alert('Select your country from the list');
  93. ucountry.focus();
  94. return false;
  95. }
  96. else
  97. {
  98. return true;
  99. }
  100. }
  101. function allnumeric(uzip)
  102. {
  103. var numbers = /^[0-9]+$/;
  104. if(uzip.value.match(numbers))
  105. {
  106. return true;
  107. }
  108. else
  109. {
  110. alert('ZIP code must have numeric characters only');
  111. uzip.focus();
  112. return false;
  113. }
  114. }
  115. function ValidateEmail(uemail)
  116. {
  117. var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  118. if(uemail.value.match(mailformat))
  119. {
  120. return true;
  121. }
  122. else
  123. {
  124. alert("You have entered an invalid email address!");
  125. uemail.focus();
  126. return false;
  127. }
  128. } function validsex(umsex,ufsex)
  129. {
  130. x=0;
  131.  
  132. if(umsex.checked)
  133. {
  134. x++;
  135. } if(ufsex.checked)
  136. {
  137. x++;
  138. }
  139. if(x==0)
  140. {
  141. alert('Select Male/Female');
  142. umsex.focus();
  143. return false;
  144. }
  145. else
  146. {
  147. alert('Form Succesfully Submitted');
  148. window.location.reload()
  149. return true;
  150. }
  151. }
  152.  
  153. USING HTML
  154. <!DOCTYPE html>
  155. <html lang="en"><head>
  156. <meta charset="utf-8">
  157. <title>JavaScript Form Validation using a sample registration form</title>
  158. <meta name="keywords" content="example, JavaScript Form Validation, Sample registration form" />
  159. <meta name="description" content="This document is an example of JavaScript Form Validation using a sample registration form. " />
  160. <link rel='stylesheet' href='js-form-validation.css' type='text/css' />
  161. <script src="sample-registration-form-validation.js"></script>
  162. </head>
  163. <body onload="document.registration.userid.focus();">
  164. <h1>Registration Form</h1>
  165. <p>Use tab keys to move from one input field to the next.</p>
  166. <form name='registration' onSubmit="return formValidation();">
  167. <ul>
  168. <li><label for="userid">User id:</label></li>
  169. <li><input type="text" name="userid" size="12" /></li>
  170. <li><label for="passid">Password:</label></li>
  171. <li><input type="password" name="passid" size="12" /></li>
  172. <li><label for="username">Name:</label></li>
  173. <li><input type="text" name="username" size="50" /></li>
  174. <li><label for="address">Address:</label></li>
  175. <li><input type="text" name="address" size="50" /></li>
  176. <li><label for="country">Country:</label></li>
  177. <li><select name="country">
  178. <option selected="" value="Default">(Please select a country)</option>
  179. <option value="AF">Australia</option>
  180. <option value="AL">Canada</option>
  181. <option value="DZ">India</option>
  182. <option value="AS">Russia</option>
  183. <option value="AD">USA</option>
  184. </select></li>
  185. <li><label for="zip">ZIP Code:</label></li>
  186. <li><input type="text" name="zip" /></li>
  187. <li><label for="email">Email:</label></li>
  188. <li><input type="text" name="email" size="50" /></li>
  189. <li><label id="gender">Sex:</label></li>
  190. <li><input type="radio" name="msex" value="Male" /><span>Male</span></li>
  191. <li><input type="radio" name="fsex" value="Female" /><span>Female</span></li>
  192. <li><label>Language:</label></li>
  193. <li><input type="checkbox" name="en" value="en" checked /><span>English</span></li>
  194. <li><input type="checkbox" name="nonen" value="noen" /><span>Non English</span></li>
  195. <li><label for="desc">About:</label></li>
  196. <li><textarea name="desc" id="desc"></textarea></li>
  197. <li><input type="submit" name="submit" value="Submit" /></li>
  198. </ul>
  199. </form>
  200. </body>
  201. </html>
  202.  
  203. USING CSS
  204. h1 {
  205. margin-left: 70px;
  206. }
  207. form li {
  208. list-style: none;
  209. margin-bottom: 5px;
  210. }
  211.  
  212. form ul li label{
  213. float: left;
  214. clear: left;
  215. width: 100px;
  216. text-align: right;
  217. margin-right: 10px;
  218. font-family:Verdana, Arial, Helvetica, sans-serif;
  219. font-size:14px;
  220. }
  221.  
  222. form ul li input, select, span {
  223. float: left;
  224. margin-bottom: 10px;
  225. }
  226.  
  227. form textarea {
  228. float: left;
  229. width: 350px;
  230. height: 150px;
  231. }
  232.  
  233. [type="submit"] {
  234. clear: left;
  235. margin: 20px 0 0 230px;
  236. font-size:18px
  237. }
  238.  
  239. p {
  240. margin-left: 70px;
  241. font-weight: bold;
  242. }
  243.  
  244.  
  245. MYSQL IN PHP
  246. <?php
  247. //Step1
  248. $db = mysqli_connect('localhost','root','root','database_name')
  249. or die('Error connecting to MySQL server.');
  250. ?>
  251. <html>
  252. <head>
  253. </head>
  254. <body>
  255. <h1>PHP connect to MySQL</h1>
  256.  
  257. <?php
  258. //Step2
  259. $query = "SELECT * FROM table_name";
  260. mysqli_query($db, $query) or die('Error querying database.');
  261.  
  262. //Step3
  263. $result = mysqli_query($db, $query);
  264. $row = mysqli_fetch_array($result);
  265.  
  266. while ($row = mysqli_fetch_array($result)) {
  267. echo $row['first_name'] . ' ' . $row['last_name'] . ': ' . $row['email'] . ' ' . $row['city'] .'<br />';
  268. }
  269. //Step 4
  270. mysqli_close($db);
  271. ?>
  272. </body>
  273. </html>
  274.  
  275. 2) <?php
  276. $servername = "localhost";
  277. $username = "username";
  278. $password = "password";
  279. $dbname = "myDB";
  280.  
  281. // Create connection
  282. $conn = new mysqli($servername, $username, $password, $dbname);
  283. // Check connection
  284. if ($conn->connect_error) {
  285. die("Connection failed: " . $conn->connect_error);
  286. }
  287.  
  288. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  289. VALUES ('John', 'Doe', 'john@example.com');";
  290. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  291. VALUES ('Mary', 'Moe', 'mary@example.com');";
  292. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  293. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  294.  
  295. if ($conn->multi_query($sql) === TRUE) {
  296. echo "New records created successfully";
  297. } else {
  298. echo "Error: " . $sql . "<br>" . $conn->error;
  299. }
  300.  
  301. $conn->close();
  302. ?>
  303.  
  304. <?php
  305. $servername = "localhost";
  306. $username = "username";
  307. $password = "password";
  308. $dbname = "myDBPDO";
  309.  
  310. try {
  311. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  312. // set the PDO error mode to exception
  313. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  314.  
  315. // prepare sql and bind parameters
  316. $stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email)
  317. VALUES (:firstname, :lastname, :email)");
  318. $stmt->bindParam(':firstname', $firstname);
  319. $stmt->bindParam(':lastname', $lastname);
  320. $stmt->bindParam(':email', $email);
  321.  
  322. // insert a row
  323. $firstname = "John";
  324. $lastname = "Doe";
  325. $email = "john@example.com";
  326. $stmt->execute();
  327.  
  328. // insert another row
  329. $firstname = "Mary";
  330. $lastname = "Moe";
  331. $email = "mary@example.com";
  332. $stmt->execute();
  333.  
  334. // insert another row
  335. $firstname = "Julie";
  336. $lastname = "Dooley";
  337. $email = "julie@example.com";
  338. $stmt->execute();
  339.  
  340. echo "New records created successfully";
  341. }
  342. catch(PDOException $e)
  343. {
  344. echo "Error: " . $e->getMessage();
  345. }
  346. $conn = null;
  347. ?>
Add Comment
Please, Sign In to add comment