Wednesday, July 30, 2014

Validation for radio button

<script>
function send()
{
var genders = document.getElementsByName("genders");
if ( (genders[0].checked == false) && (genders[1].checked == false) && (genders[2].checked == false) && (genders[3].checked == false)) {
alert("it is empty");
return false;
}
return true;
}
</script>

Disable KeyDown And Ctrl key

<script>
document.onkeydown = function() {
    alert();   
    switch (event.keyCode) {
        case 116 : //F5 button
            event.returnValue = false;
            event.keyCode = 0;
            return false;
        case 82 : //R button
            if (event.ctrlKey) {
                event.returnValue = false;
                event.keyCode = 0; 
                return false;
            }
    }
}
</script>

Disable Right Click In all browser's

<script language="javascript">

                   var message="This function is not allowed here.";
                   function clickIE4(){

                                 if (event.button==2){
                                 alert(message);
                                 return false;
                                 }
                   }

                   function clickNS4(e){
                                 if (document.layers||document.getElementById&&!document.all){
                                                if (e.which==2||e.which==3){
                                                          alert(message);
                                                          return false;
                                                }
                                        }
                   }

                   if (document.layers){
                                 document.captureEvents(Event.MOUSEDOWN);
                                 document.onmousedown=clickNS4;
                   }

                   else if (document.all&&!document.getElementById){
                                 document.onmousedown=clickIE4;
                   }

                   document.oncontextmenu=new Function("alert(message);return false;")

    </script>

Tuesday, July 29, 2014

Script to stop the function of Right click and F5

<script language="javascript" type="text/javascript">
window.history.forward(1);
document.attachEvent("onkeydown", my_onkeydown_handler);
function my_onkeydown_handler()
{
switch (event.keyCode)
{
case 116 : // 'F5'
event.returnValue = false;
event.keyCode = 0;
//window.status = "We have disabled F5";
break;
}
}
document.onmousedown=disableclick;
status="Right Click is not allowed";
function disableclick(e)
{
if(event.button==2)
{
alert(status);
return false;
}
}
</script>

NOTE:-Works only in IE.

Disable refresh key(F5)

Use the below code to disable your F5 key on browser:-

document.onkeydown = function() {
        if(event.keyCode == 116) {
                event.returnValue = false;
                event.keyCode = 0;
                return false;
               }
    }



Put this code simply in the script.(NOTE:- Works only in IE).

Monday, July 28, 2014

From validation using javascript

<script type="text/javascript">
function checkForm(reg_form)
{
if(reg_form.name.value == "")
{
alert("Error: Name cannot be blank!");
reg_form.name.focus();
return false;
}
re = /^\w+$/;
if(!re.test(reg_form.name.value))
{
alert("Error: Name must contain only letters, numbers and underscores!");
reg_form.name.focus();
return false;
}
if(reg_form.u_rollno.value == "")
{
alert("Error: university rollno cannot be blank!");
reg_form.name.focus();
return false;
}
re = /^\w+$/;
if(!re.test(reg_form.name.value))
{
alert("Error: University roll no must contain only letters, numbers and underscores!");
reg_form.name.focus();
return false;
}

if(reg_form.sem.value == "")
{
alert("Error: Semester cannot be blank!");
reg_form.name.focus();
return false;
}
re = /^\w+$/;
if(!re.test(reg_form.sem.value))
{
alert("Error: Semester must contain only letters, numbers and underscores!");
reg_form.sem.focus();
return false;
}
if(reg_form.branch.value == "")
{
alert("Error: branch cannot be blank!");
reg_form.branch.focus();
return false;
}
re = /^\w+$/;
if(!re.test(reg_form.branch.value))
{
alert("Error: branch must contain only letters, numbers and underscores!");
reg_form.branch.focus();
return false;
}
if(reg_form.c_id.value == "")
{
alert("Error: College id cannot be blank!");
reg_form.c_id.focus();
return false;
}

var x = document.forms["reg_form"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Not a valid e-mail address");
        return false;
    }

if(reg_form.password.value != "" && reg_form.password.value == reg_form.c_password.value)
{
if(reg_form.password.value.length < 6)
{
alert("Error: Password must contain at least six characters!");
reg_form.password.focus();
return false;
}
if(reg_form.password.value == reg_form.name.value)
{
alert("Error: Password must be different from Username!");
reg_form.password.focus();
return false;
}
re = /[0-9]/;
if(!re.test(reg_form.password.value))
{
     alert("Error: password must contain at least one number (0-9)!");
     reg_form.password.focus();
     return false; }
     re = /[a-z]/;
if(!re.test(reg_form.password.value))
{
     alert("Error: password must contain at least one lowercase letter (a-z)!");
     reg_form.password.focus();
     return false;
}
re = /[A-Z]/;
if(!re.test(reg_form.password.value))
{
    alert("Error: password must contain at least one uppercase letter (A-Z)!");
    reg_form.password.focus();
    return false;
}
}
else
{
     alert("Error: Please check that you've entered and confirmed your password!");
     reg_form.password.focus();
     return false;
}
}
</script>
<center>
<table  cellpadding="10" cellspacing="10">
<form action="reg_action.php" onSubmit="return checkForm(this);" method="post" name="reg_form">
<tr>
<td>Name:</td>
<td><input type="text" size="20" name="name" /></td>
</tr>
<tr>
<td>University Roll No:</td>
<td><input type="text" size="20" name="u_rollno" /></td>
</tr>
<td>Semester:</td>
<td><input type="text" size="20" name="sem" /></td>
</tr>
<tr>
<td>Branch:</td>
<td><input type="text" size="20" name="branch" /></td>
</tr>
<tr>
<td>College id:</td>
<td><input type="text" size="20" name="c_id"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" size="20" name="email" /></td>
</tr>
<tr>
<td>Phone No:</td>
<td><input type="text" size="20" name="phone_no" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" size="20" name="password" /></td>
</tr>
<tr>
<td>Conform Pssword:</td>
<td><input type="password" size="20" name="c_password" /></td>
</tr>
<tr>
<tr>
<td></td>
<td><input type="submit" value="REGISTER" >  <input type="reset" value="RESET"></td>
</tr>
</form>
</table>
</center>
</html>

Submit Form on Refresh or move to another Page

<script type="text/javascript">
 function finishpage()
{
alert("unload event detected!");
document.quiz.submit();
}
window.onbeforeunload= function() {
setTimeout('document.quiz.submit()',1);
}
</script>

Friday, July 25, 2014

How to submit an form after an time interval?

<?php
include("config.php");
$sql=mysql_query("select * from timer");
$row_2=mysql_fetch_array($sql);
$row_2['num_timer'];
?>


 <div style="font-weight: bold" id="quiz-time-left"></div>

<script type="text/javascript">
var max_time = <?php echo $row_2['num_timer'] ?>;
var c_seconds  = 0;
var total_seconds =60*max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
if(total_seconds <=0){
setTimeout('document.quiz.submit()',1);
  
    } else
    {
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}

}
init();
</script>


<form method="post" name="quiz" id="quiz_form" action="query.php" >
</form>

Auto submit (onload) a HTML Form

<form id="myForm" name="myForm" action="http://example.com/examplePage.do" method="POST">
<input type=text name="val1" id="val1" value="value1"/>
<input type=text name="val2" id="val2" value="value2"/>
<input type=text name="val3" id="val3" value="value3"/>
<input type=text name="submit" id="submit" value="Continue"/>
</form>



 Use the below code:-




<html>
<body onload="document.createElement('form').submit.call(document.getElementById('myForm'))">
<form id="myForm" name="myForm" action="http://example.com/examplePage.do" method="POST">
<input type=hidden name="val1" id="val1" value="value1"/>
<input type=hidden name="val2" id="val2" value="value2"/>
<input type=hidden name="val3" id="val3" value="value3"/>
<input type=hidden name="submit" id="submit" value="Continue"/>
</form>
</body>
</html>

Friday, July 18, 2014

How to Import large sql file to WAMP/phpmyadmin?

Step 1: Find the config.inc.php file located in the phpmyadmin directory. In my case it is located here:

C:\wamp\apps\phpmyadmin3.4.5\config.inc.php 

Note: phymyadmin3.4.5 folder name is different in different version of wamp

Step 2: Find the line with $cfg['UploadDir'] on it and update it to:

$cfg['UploadDir'] = 'upload';

Step 3: Create a directory called ‘upload’ within the phpmyadmin directory.

C:\wamp\apps\phpmyadmin3.2.0.1\upload\

Step 4: Copy and paste the large sql file into upload directory which you want importing to phymyadmin

Step 5: Select sql file from drop down list from phymyadmin to import.