Loading
Tampilkan postingan dengan label cek. Tampilkan semua postingan
Tampilkan postingan dengan label cek. Tampilkan semua postingan

29 Maret 2010

Javascript Checkbox Checked / Javascript Check All Checkbox

Kalian tahu kan fungsi checkall? Yupz, ini adalah fungsi yang digunakan untuk mencentang semua kolom cek list ataupun sebaliknya untuk menghilangkan semua centang yang ada pada kolom cek list. Untuk membuat fungsi ini kita hanya memerlukan sebuah script javascript yang digunakan untuk mencentang semua cek list maupun menghilangkan semua centang pada cek list.
Secara keseluruhan script tersebut seperti dibawah ini:

<html>
<body>
<center>
<table border="1px" width="180">
<tr>
<td>
<ul>
<li>
<input type="checkbox" name="check_ctrl" onClick="Check(document.myform.check_list)" /> <b id=message>Check All</b>
</li>
<li><input type="button" name="button" id="button" onClick="Check(document.myform.check_list)" value="Check All" /></li>
</ul>
<hr>
<ol>
<li><input type="checkbox" name="check_list" value="1"> ASP</li>
<li><input type="checkbox" name="check_list" value="2"> PHP</li>
<li><input type="checkbox" name="check_list" value="3"> JavaScript</li>
<li><input type="checkbox" name="check_list" value="4"> HTML</li>
<li><input type="checkbox" name="check_list" value="5"> MySQL</li>
</ol>
</td>
</tr>
</table>
</form>
<script type="text/javascript" language="javascript">
<!--
function Check(chk)
{
if(document.myform.check_ctrl.checked==true || document.getElementById("button").value=="Check All")
{
for(i=0;i<chk.length;i++)
chk[i].checked=true ;
document.getElementById("message").innerHTML="UnCheck All";
document.getElementById("button").value="UnCheck All";
}
else
{
for(i=0;i<chk.length;i++)
chk[i].checked=false;
document.getElementById("message").innerHTML="Check All";
document.getElementById("button").value="Check All";
}
}
//-->
</script>
</center>
</body>
</html>



Untuk melihat demonya silahkan kunjungi:
[Demo @ catment.co.cc/demo/checkall]
Download scriptnya:
http://catment.co.cc/download/check_all.zip

1 Maret 2010

Check Javascript Browser Enabled / Disabled

Javascript biasa digunakan untuk membuat suatu validasi dari suatu form input karena javascript merupakan bahasa pemrograman web yang bersifat client side, yaitu bahasa pemrograman yang proses eksekusi perintahnya dilakukan pada sisi client, sehingga membuat validasi dengan javascript tanpa perlu mereload suatu halaman. Tetapi javascript memiliki suatu kelamahan. Dalam hal validasi seseorang bisa saja memasukan data yang tidak sesuai dengan aturan validasi yang dibuat dengan script javascript, untuk memasukan data yang tidak valid tersebut user cukup mematikan / disable javascript browser yang digunakan, selanjutnya user dapat menginputkan data yang tidak sesuai dengan aturan validasi yang dibuat. Bayangkan jika validasi extensi file upload bisa diabaikan..? Jika validasi extensi file upload tersebut hanya memperbolehkan seorang user mengupload file extensi jpg, dan user tersebut me- nonaktif / disable javascript browser yang digunakan, maka ia dapat mengupload file dengan extensi apa saja misalkan exe. Bayangkan jika user tersebut mengupload sebuah virus?

Untuk mengatasi masalah tersebut kita dapat membuat suatu aturan yang mengharuskan user meng- aktif / enable javascript browser yang digunakan, jika javascript browser disable / nonaktif kita dapat meng-kick user tersebut sehingga ia tidak dapat memasukan data jika javascript browser disable / nonaktif. Untuk dapat membuat aturan tersebut kita harus dapat melakukan check javascript browser enabled / disabled dengan membuat suatu script yang akan disisipkan ditiap halaman atau dihalaman yang anda anggap perlu, dan sebuah halaman kita buat sebagai output dari user yang kita kick.

- Script yang disisipkan:


<noscript>
<meta http-equiv="refresh" content="0;URL=js_disable.php" />
</noscript>


js_disable.php adalah halaman tujuan kick dari user yang nonaktif / disable javascript browser yang digunakannya.

- js_disable.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Javascript Disabled</title>
</head>
<body>
<center>
<noscript>
<h3>Javascript anda tidak aktif mohon aktifkan / enabled javascript browser anda.</h3>
</noscript>
</center>
</body>
</html>

Dengan begitu user yang nonaktif / disable javascript browser yang digunakannya akan dialihkan kehalaman js_disable.php dan diberikan sebuah peringatan untuk meng- aktif / enable javascript browser yang digunakannya.
[DOWNLOAD SCRIPT DISINI]

23 Februari 2010

Javascript For Email Validation (Javascript Untuk Validasi Email)








When validating an e-mail address, you are looking for the typical format found in such addresses. There may be some domain names that are more than three characters, but it isn't typical. Also, just because the user types what looks like a valid e-mail address, that does not mean that it is; for example, the e-mail address santa@northpole.org uses a valid syntax, but that fact does not prove that santa is a real user.
E-mail addresses usually have the following format:


  • An @ sign between the username and address (lequig@aol.com)


  • At least one dot between the address and domain name ( .com, .mil, .edu, .se)


  • At least six characters (a@b.se)[3]
    [3] As of this writing, domain names have at least two characters.
Examples of valid e-mail addresses:
username@mailserver.com
username@mailserver.info
username@mailserver.org.se
username.moretext@mailserver.mil
username@mailserver.co.uk
user-name.moretext.sometext@mailserver.se
To break down the regular expression
/^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
use the following steps:
Step 1:
^
Go to the beginning of the line.
Step 2:
([\-\w]+)\.?
The user name consists of one or more dashes or word characters grouped by parentheses, followed by one (or not one) literal period. Because the dot is outside the parentheses there will be either one or zero dots for the list of word characters, not two or three dots in a row.
Step 3:
(([\-\w]+)\.?)+
The user name may consist of more than one set of word characters separated by a single dots, as in Joe.Shmoe.somebody.
Step 4:
@
A literal @ symbol is required in the e-mail address.
Step 5:
([\-\w]+)\.?)+
The mail server's name is like the user's name, a group of word characters separated by a dot. Same as step 3.
Step 6:
[a-zA-Z]{2,4}
The domain name follows the mail server's name. A single dot separates the server from the domain. The domain name consists of between two and four alphabetic characters; e.g., savageman@imefdm.usmc.mil or patricia.person@sweden.sun.com.
Step 7:
$
The end of the line anchor assures that no extra characters can be added onto the end of the e-mail address.




<body>
<h2> Checking for Valid Email Address </h2>
<h2><?php echo "$user_email"; ?></h2>
<form name="formtest" action="#" method="post" onSubmit="return ok_Email(this);">
<p>
Please enter your email address: <BR>
<input type="text" size=40 name="user_email">
</p>
<p>
<input type=submit value="Send">
</p>
</form>
</body></html>


[Download Sript]

EXPLANATION


  1. A function called ok_Email is defined. It takes one parameter, a reference to the form started on line 6.


  2. The regular expression is assigned the variable regex. The regular expression reads: Start at the beginning of the string (^), look for one or more alphanumeric characters and/or a dash followed by a literal dot (or not one). The literal period is outside the parentheses, meaning that in each group of word characters, there will be only one (or not one) period; e.g., abc.xyz. The entire expression is in parentheses followed by a + sign. This means that the pattern can be repeated one or more times; e.g., abc.xyz.yaddy.yady.yady. Next comes a literal @ symbol, required in all e-mail addresses. The mail server name comes right after the @ sign. Like the user name, it is represented by one or more alphanumeric characters or a dash, followed by a literal dot (\.). Now we have Joe.Blow@aol or DanSav@stamford, etc. This pattern, like the first pattern, can be repeated one or more times. The domain name part of the address comes next; a literal dot, and at least two but not more than four alphabetic characters, [a-zA-Z]{2,4}; e.g., JoeBlow@Chico.com, danny@Stamford.edu, .se, .uk, etc. There are other varieties that could also be considered, such as john@localhost, but for most e-mail addresses, the regular expression used in this example should suffice.


  3. The regular expression test() method takes the value of the user input, user_email.value, and returns true if the pattern in the regular expression matched the user's input.


  4. The e-mail address entered is tested to be valid. A true value is returned and the form will be submitted to the server. A valid e-mail address does not mean that if mail is sent to that address it will necessarily be delivered; e.g., santa@northpole.org is syntactically valid, but there is no guarantee that santa is a real user (unless you still believe!).


  5. If an invalid e-mail address was entered, the alert box will appear with this message. The ok_Email() function will return false, and the form will not be submitted.


  6. The form named formtest starts here.


  7. This is the URL of the CGI script that will be called on the server side when the form is submitted.


  8. The onSubmit event handler is triggered when the user presses the submit button. The value assigned to the event is a function called ok_Email that will return true if the e-mail address is valid and false, if not. The form will be sent to the server only if the return value is true.