Form Validation Like Ajax or JQuery Using Simple Javascript and CSS
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.
-
<html>
-
<head>
-
<script type="text/javascript"><!–
-
function update() {
-
if(document.form.num_1.value != '1')
-
document.form.sum.value = 'Start with 1';
-
document.form.sum.style.visibility = 'visible';
-
}
-
//–></script>
-
</head>
-
-
<body>
-
<form name="form" method="post" action="">
-
<table width="100%" border="0" cellpadding="1">
-
<tr>
-
<td>First Number </td>
-
<td><input name="num_1" type="text" id="num_1" onChange="update()">
-
<input name="sum" type="text" id="sum" readonly="" style="border:0px; visibility:hidden; "></td>
-
<td> </td>
-
<td> </td>
-
</tr>
-
<tr>
-
<td>Second Number </td>
-
<td colspan="3"><input name="num_2" type="text" id="num_2" onChange="update()"></td>
-
</tr>
-
</table>
-
</form>
-
</body>
I hope logic can be developed by you for further uses.
Converting Numerical Value to its Corresponding Text Format!
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.
-
$nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
-
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
-
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
-
"nineteen", "twenty", 30 => "thirty", 40 => "forty",
-
50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
-
90 => "ninety" );
-
-
function int_to_words($x) {
-
global $nwords;
-
-
if(!is_numeric($x))
-
$w = '#';
-
else if(fmod($x, 1) != 0)
-
$w = '#';
-
else {
-
if($x < 0) {
-
$w = 'minus ';
-
$x = -$x;
-
} else
-
$w = '';
-
// … now $x is a non-negative integer.
-
-
if($x < 21) // 0 to 20
-
$w .= $nwords[$x];
-
else if($x < 100) { // 21 to 99
-
$w .= $nwords[10 * floor($x/10)];
-
$r = fmod($x, 10);
-
if($r > 0)
-
$w .= '-'. $nwords[$r];
-
} else if($x < 1000) { // 100 to 999
-
$w .= $nwords[floor($x/100)] .' hundred';
-
$r = fmod($x, 100);
-
if($r > 0)
-
$w .= ' and '. int_to_words($r);
-
} else if($x < 1000000) { // 1000 to 999999
-
$w .= int_to_words(floor($x/1000)) .' thousand';
-
$r = fmod($x, 1000);
-
if($r > 0) {
-
$w .= ' ';
-
if($r < 100)
-
$w .= 'and ';
-
$w .= int_to_words($r);
-
}
-
} else { // millions
-
$w .= int_to_words(floor($x/1000000)) .' million';
-
$r = fmod($x, 1000000);
-
if($r > 0) {
-
$w .= ' ';
-
if($r < 100)
-
$word .= 'and ';
-
$w .= int_to_words($r);
-
}
-
}
-
}
-
return $w;
-
}
-
-
//Where should you need to have the text format
-
-
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




