Tuesday, 2 February 2021

JavaScript validations || Form validation name, mobile, email, password using javaScript

 JAVA SCRIPT VALIDATIONS

NAME, USER ID, PASSWORD VALIDATION USING JAVASCRIPT EXAMPLE

Example:

<script>

function validation()

{

var pass = form.pass.value;

var pass2= form.pass2.value;

if(document.form.name.value=="")

{

alert("Please Enter Your Name");

return false;

}

if(document.form.userid.value.length<=3) 

{

alert("PLEASE ENTER VALID USER ID ");

return false;

}

if(document.form.userid.value.length>10)

{

alert("Please enter UserId Should less Character Length");

return false;

}

if(pass != pass2)

{

alert("Both Password Are Not Matched");

return false;

}


if(!document.form.agree.checked)

{

alert("Please Check agree button");

return false;

}

return true;

}

</script>


<form name="form" onSubmit="return validation();" method="post" action="testing.html">

<table width="322" border="1">

  <tr>

    <td width="147">Name</td>

    <td width="159"><input type="text" name="name" value=""></td>

  </tr>

  <tr>

    <td>User Id</td>

    <td><input type="text"  name="userid" value=""></td>

  </tr>

  <tr>

    <td>Password</td>

    <td> <input type="text" name="pass" value=""></td>

  </tr>

  <tr>

    <td>Confirm Password </td>

    <td><input type="text" name="pass2" value=""></td>

  </tr>

  <tr>

    <td>Agree</td>

    <td><input type="checkbox" name="agree"></td>

  </tr>

  <tr>

    <td><input  type="submit" value="    Register    "></td>

    <td><input type="reset" value=" Field Blanker  "></td>

  </tr>

</table>

</form>


__________________________________________________

JAVA SCRIPT EMAIL VALIDATION:

        <input id="textEmail">

<button type="button" onclick="myFunction()">Submit</button>

<p id="demo" style="color: red;"></p>

    <script>

    function myFunction() {

        var email;

        email = document.getElementById("textEmail").value;

            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

            if (reg.test(textEmail.value) == false) 

            {

                document.getElementById("demo").style.color = "red";

                document.getElementById("demo").innerHTML ="Invalid EMail ->"+ email;

                alert('Invalid Email Address ->'+email);

                return false;

            } else{

            document.getElementById("demo").style.color = "DarkGreen";      

            document.getElementById("demo").innerHTML ="Valid Email ->"+email;

            }

       return true;

    }

    </script>


_________________________________________________

ENTER ONLY NUMBERS VALIDATION USING JAVASCRIPT


<script>

function num()

{

n = frm.nm.value; 

if(isNaN(n)) //none of numeric character

{

alert("Please Enter Only Numbers");

return false;

}

else if(frm.nm.value.length<10)

{

alert("Enter Proper Mobile Number");

return false;

}

else

{

alert("Success");

return true;

}

}

</script>

<form onsubmit="return num();" name=frm>

<input type=text name=nm value="" placeholder="Mobile">

<br>

<input type=submit value="  Confirm ">

</form>


__________________________________________________

Learn All Programming and Scripting Languages with youtube.com/avadhtutor



No comments:

Post a Comment