Showing posts with label JAVASCRIPT. Show all posts
Showing posts with label JAVASCRIPT. Show all posts

JavaScript Form Validation

WHY CLIENT SIDE FORM VALIDATION???
Forms validation on the client-side is essential — it saves time and bandwidth, and gives you more options to point out to the user where they’ve gone wrong in filling out the form. But apart from this you should also use server side validation. because people visit your site may use an old browser or have JavaScript disabled, which will break client-only validation. Client and server-side validation complement each other, and as such, they really shouldn’t be used independently.

WHY IS CLIENT SIDE FORM VALIDATION IS GOOD?
It’s a fast form of validation: If something’s wrong, the event is triggered upon submission of the form or on change of focus.
You can safely display only one error at a time and focus on the wrong field, to help ensure that the user correctly fills in all the details you need.

The two key approaches to client-side form validation are:
1- Display the errors one by one, focusing on the offending field.
2- Display all errors simultaneously, server-side validation style.

While displaying all errors simultaneously is required for server-side validation, the better method for validation on the client-side is to show one error at a time. This makes it possible to highlight only the field that has been incorrectly completed, which in turn makes revising and successfully submitting the form much easier for the visitor. If you present users with all errors at the same time, most people will try to remember and correct them at once, instead of attempting to re-submit after each correction.
Here is the HTML code for the form validation:-
<html> <head> <title>Java script form validation</title> <script type=”text/javascript”> function form_validate() { if( document.Form.Name.value == “” ) { alert( “Please enter your first name.” ); document.Form.Name.focus() ; return false; } if( document.Form.EMail.value == “” ) { alert( “Please enter your E-mail address.” ); document.Form.EMail.focus() ; return false; } if( document.Form.Zip.value == “” || isNaN( document.Form.Zip.value ) || document.Form.Zip.value.length != 5 ) { alert( “Please enter a zip in the format *****.” ); document.Form.Zip.focus() ; return false; } } </script> </head> <body> <form name=”Form” onsubmit=”return(form_validate());”> <table> <tr> <td>First Name:</td> <td><input type=”text” name=”Name” /></td> </tr> <tr> <td>E-Mail Address:</td> <td><input type=”text” name=”EMail” /></td> </tr> <tr> <td>Postal Code:</td> <td><input type=”text” name=”Zip” /></td> </tr> <tr> <td></td> <td><input type=”submit” value=”Submit” /></td> </tr> </table> </form> </body> </html>

Hope this php tutorial is useful for you. Keep following Php Point for more Codes.
Continue Reading

JavaScript Email validation Using Regular Expression

In this tutorial of Php Point we will show you a simple way of validating email address using javascript and Regular Expression

It is the simple code to help you in creating a form with email validation using regular expression.
<html>
    <head>
        <title>JavaScript form validation Using regular expression</title>
        <script type= “text/javascript”>
            function Validate(input)
            {
                var format = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
                if(document.getElementById(“email”).value.match(format))
                {
                    document.myform.email.focus();
                    return true;
                }
                else
                {
                    alert(“You have entered an wrong email address!”);
                    document.myform.email.focus();
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <h2>Input an email and Submit</h2>
        <form name=”myform” action=”#”>
            <input type=’text’ name=’email’ id=”email”/>
            <input type=”submit” name=”submit” value=”Submit”   onclick=”Validate(document.myform.email)”/>
        </form>
    </body>
</html>
Hope this php tutorial is useful for you. Keep following Php Point for more Codes.
Continue Reading

Disable Right Click using JavaScript


In this tutorial of Php Point we will show you how to disable right click using javascript.

Here is the simple javascript code to disable right click. Just add on your head tag and right click will be disabled on that page.
<script src=”http://code.jquery.com/jquery-1.9.1.js”></script>
<script type=”text/javascript” language=”javascript”>
     $(function() {
            $(this).bind(“contextmenu”, function(e) {
                e.preventDefault();
            });
        });
</script>
Continue Reading