Browsing articles from "August, 2009"

Form Validation Like Ajax or JQuery Using Simple Javascript and CSS

Aug 5, 2009   //   by nuhil   //   Ajax, CSS, JQuery, JS  //  No Comments

The code below will create a form with two fields. If do not enter “1″ in the first form field try to leave that field then it will response and create a message on the right side of the field.

This will work just like any Ajax or Jquery form validation but it is simply Javascript and CSS.

  1. <html>
  2. <head>
  3. <script type="text/javascript"><!–
  4. function update() {
  5. if(document.form.num_1.value != '1')
  6. document.form.sum.value = 'Start with 1';
  7. document.form.sum.style.visibility = 'visible';
  8. }
  9. //–></script>
  10. </head>
  11.  
  12. <body>
  13. <form name="form" method="post" action="">
  14. <table width="100%"  border="0" cellpadding="1">
  15. <tr>
  16. <td>First Number </td>
  17. <td><input name="num_1" type="text" id="num_1"  onChange="update()">
  18. <input name="sum" type="text" id="sum" readonly="" style="border:0px; visibility:hidden; "></td>
  19. <td>&nbsp;</td>
  20. <td>&nbsp;</td>
  21. </tr>
  22. <tr>
  23. <td>Second Number </td>
  24. <td colspan="3"><input name="num_2" type="text" id="num_2"  onChange="update()"></td>
  25. </tr>
  26. </table>
  27. </form>
  28. </body>

I hope logic can be developed by you for further uses.

Converting Numerical Value to its Corresponding Text Format!

Aug 3, 2009   //   by nuhil   //   PHP  //  2 Comments

Some times we need to convert a numerical value to its text value. For example when we want to make a digit or a summation of some value to a text format in the case of any accounting purpose then this code of functions will help.

  1. $nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
  2. "eight", "nine", "ten", "eleven", "twelve", "thirteen",
  3. "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
  4. "nineteen", "twenty", 30 => "thirty", 40 => "forty",
  5. 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
  6. 90 => "ninety" );
  7.  
  8. function int_to_words($x) {
  9. global $nwords;
  10.  
  11. if(!is_numeric($x))
  12. $w = '#';
  13. else if(fmod($x, 1) != 0)
  14. $w = '#';
  15. else {
  16. if($x < 0) {
  17. $w = 'minus ';
  18. $x = -$x;
  19. } else
  20. $w = '';
  21. // … now $x is a non-negative integer.
  22.  
  23. if($x < 21)   // 0 to 20
  24. $w .= $nwords[$x];
  25. else if($x < 100) {   // 21 to 99
  26. $w .= $nwords[10 * floor($x/10)];
  27. $r = fmod($x, 10);
  28. if($r > 0)
  29. $w .= '-'. $nwords[$r];
  30. } else if($x < 1000) {   // 100 to 999
  31. $w .= $nwords[floor($x/100)] .' hundred';
  32. $r = fmod($x, 100);
  33. if($r > 0)
  34. $w .= ' and '. int_to_words($r);
  35. } else if($x < 1000000) {   // 1000 to 999999
  36. $w .= int_to_words(floor($x/1000)) .' thousand';
  37. $r = fmod($x, 1000);
  38. if($r > 0) {
  39. $w .= ' ';
  40. if($r < 100)
  41. $w .= 'and ';
  42. $w .= int_to_words($r);
  43. }
  44. } else {    //  millions
  45. $w .= int_to_words(floor($x/1000000)) .' million';
  46. $r = fmod($x, 1000000);
  47. if($r > 0) {
  48. $w .= ' ';
  49. if($r < 100)
  50. $word .= 'and ';
  51. $w .= int_to_words($r);
  52. }
  53. }
  54. }
  55. return $w;
  56. }
  57.  
  58. //Where should you need to have the text format
  59.  
  60. echo 'There are currently '. int_to_words(100) . ' members logged on.';

The parameter “100″ will be processed by the functions and will output a value : one hundred