Labels

php (35) javascript (31) phpjavascript (30) jquery (23) html (20) mysql (14) database (9) codeigniter (4) json (4) bar chart (2) calendar (2) column chart (2) framework (2) google maps (2) query (2) tables (2) url (2) dropdown (1)

Thursday, March 22, 2018

Form Validation Using Jquery

The code that every web developer looking for. Validating a form in all possible ways. A simple and effective code is here. 

Here we are validating all common input types which we use in register form like name, email, phone, password, radio button, select option.

Includes

    <link rel="stylesheet" href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css'>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script>



Script

<script>
function Validate() {

(function ($) {

 jQuery.validator.setDefaults({
       errorPlacement: function (error, element) {
            error.appendTo('#invalid-' + element.attr('id'));
       }
});

//To validate letters

$.validator.addMethod("letters", function (value, element) {
   return this.optional(element) || value == value.match(/^[a-zA-Z\s]*$/);
});



//To validate Number
$.validator.addMethod("number", function (value, element) {
      return this.optional(element) || value == value.match(/^[0-9]*$/);
});

//To validate digits for phone number that should start with only 7 or 8 or 9
$.validator.addMethod("digits", function (value, element) {
    return this.optional(element) || value == value.match(/^^[789]\d{9}$/);
});

//To validate password
$.validator.addMethod("pwcheckNumber",
   function (value, element) {
   return /\d/.test(value);
});

$.validator.addMethod("pwcheckUppercase",
   function (value, element) {
   return /[A-Z]/.test(value);
});

$.validator.addMethod("pwcheckSpecial",
     function (value, element) {
     return /[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/.test(value);
});

//Form validation
$("#Form").validate({
                    
rules: {

Title: {
     required: true,                  
},

FirstName: {
   required: true,
   minlength: 3,
   letters: true                      
},

Mobile: {
    required: true,
    number: true,
    minlength: 10,
    maxlength: 10,
    digits: true                      
},

Email: {
    required: true,
    email: true,
},

Password: {
     required: true,
     minlength: 8,
     pwcheckNumber: true,
     pwcheckUppercase: true,
     pwcheckSpecial: true,          
},

ConfirmPassword: {
     required: true,
     equalTo: "#Password"                   
},

Gender: {
     required: true,
}
     
},

//Message suggestions that you see on screen     
messages: {

Title: {
       required: "Please select title",
},

FirstName: {
      required: "Please enter first name",
      minlength: "Name should be minimum 3 characters",
      letters: "Only letters and spaces are allowed",                              
},

Mobile: {
       required: "Please enter  phone number",
       minlength: "Please enter valid phone number",
       maxlength: "Please enter valid phone number",
       digits: "Phone number should be start with 789", 
},

Email: {
      required: "Please enter email",
      minlength: "Please enter a valid email address",
},

Password: {
       required: "Please enter password",
       minlength: "Password must be minmum 8 character",
       pwcheckNumber: "Password must contains one digit",
       pwcheckUppercase: "Password must contains atleast one uppercase letter",
       pwcheckSpecial: "Password must contains atleast one special character",
},

ConfirmPassword: {
       required: "Please enter confirm password ",
       equalTo: "Confirm password must same as password",
},

Gender: {
       required: "Please select gender type"                     
}
}

});
})($);
}
</script>

//CSS
<style>
.error_msg {
        color: red;
}

body{
        position: absolute;
        width: 600px;
        height: 200px;
        z-index: 15;
        top: 15%;
        left: 30%;
        margin: -100px 0 0 -150px;
     }

.box-header{
        background: #dc3545;
        width: 500px;
        height: auto;
text-align: center;
        color: white;
        font-size: 30px;
        padding: 10px;
    }

.box{
        background: #43c181;
        padding-top: 10px;
        padding-left: 110px;
        padding-bottom: 30px;
        width: 500px;
        height: auto;
    }
</style>




<body onload="Validate()">

<div class="box-header">Register From</div>

<div class="box">

    <form action='#' method='post' id='Form' enctype='multipart/form-data'>
        <div class="col-md-8">
            <label>Title</label>
            <select id="Title" name="Title" class="form-control">
                <option value="">Select Title</option>
                <option value="Mr.">Mr.</option>
                <option value="Mrs.">Mrs.</option>
                <option value="Ms.">Ms.</option>
                <option value="Mss.">Mss.</option>
                <option value="Dr.">Dr.</option>
            </select>
            <div id="invalid-Title" class="error_msg"></div>
        </div>

        <div class="col-md-8">
            <label>First Name</label>
            <input type="text" id="FirstName" name="FirstName" class="form-control"
                   placeholder="First Name"/>
            <div id="invalid-FirstName" class="error_msg"></div>
        </div>

        <div class="col-md-8">
            <label>Mobile</label>
            <input type="text" id="Mobile" name="Mobile" class="form-control" placeholder="Mobile">
            <div id="invalid-Mobile" class="error_msg"></div>
        </div>

        <div class="col-md-8">
            <label>Email</label>
            <input type="text" id="Email" name="Email" class="form-control" placeholder="Email">
            <div id="invalid-Email" class="error_msg"></div>
        </div>

        <div class="col-md-8">
            <label>Password</label>
            <input type="password" id="Password" name="Password" class="form-control"
                   placeholder="Password"/>
            <div id="invalid-Password" class="error_msg"></div>
        </div>

        <div class="col-md-8">
            <label>Confirm Password</label>
            <input type="password" id="ConfirmPassword" name="ConfirmPassword" class="form-control"
                   placeholder="ConfirmPassword"/>
            <div id="invalid-ConfirmPassword" class="error_msg"></div>
        </div>

        <div class="col-md-8">
            <label>Gender</label>
            <input type="radio" id="Gender" name="Gender" value="Male"> Male
            <input type="radio" id="GenderNew" name="Gender" value="Female"> Female<br>
            <div id="invalid-Gender" class="error_msg"></div>
        </div>

        <div class="col-md-12" align="center">
            <input type="submit" value="Submit" class="btn btn-danger" id="Submit" name="Submit"
                   onclick="Register()">
        </div>
    </form>

</div>

</body>


Result



*Please share, comment and subscribe to PHP Javascript for more.

Display JSON data in dropdown

Display JSON data in a table



26 comments:

  1. Thank for sharing with us.
    Waiting for a revolutionary and an outstanding Carpet shampooer! Expecting such a product form Bissell as usual? To meet up your expectations, the company has introduced Bissell preheat 2x revolution this time that is great for deep cleaning of your carpets and for making them new. So, delay no further to read about Bissell Proheat 2x Revolution reviews.

    ReplyDelete
  2. Buy perfect money in India @Rs.70/- per pm Sell @Rs.58/- per pm,CALL 08054019105, Chandigarh, 160017

    --------------------------------------------------------------------------------------------------------------

    Buy/Sell Perfect Money At Reasonable Rates. We Provide Perfect Money At best Rates.
    We also sell buy Perfect Money, neteller and Bitcoin.

    Call us : +91 9517544444

    Gtalk : garrysingh1598

    Web.: www.sellperfectmoney64.com

    ReplyDelete
  3. Assignment Help
    All Assignment Help is an online Assignment Help and assignment writing service offering expert assignment help.Hire Australian,US assignment writing experts.

    ReplyDelete
  4. AllAssignmentHelp Provider is helping students especially in usa in getting brilliant quality reviews writing USA, essays and dissertations.We at Top Quality Assignment believe that there is no shortcut to success and to attain success, hard work, dedication, and commitment must be present.AllAssignmentHelp reviewsbest in writing unique Assignment.

    ReplyDelete
  5. Situs Poker Terpercaya Agen Judi Poker Online Indonesia Terpercaya Domino Qiu Serta melengkapinya Permainan Ceme Online,Capsa Susun Deposit 10rb. Situs Poker Terpercaya

    ReplyDelete
  6. A superior all assignment Help reviews offered by this website with the advantage of online support with high proficiency level based on its latest research and information by professional reviews writers. Wide ranges of
    are covered with separate writers for each subject.
    All Assignment Help review

    ReplyDelete
  7. Hi! your blog is very nice. I read it completely with the deep eye. really your blog is knowledge gaining. I really appreciate your blog. you have really done a great work. please keep it up.You have given such a great information that will be very useful for me. I want to know more information deeply about it. If possible you can share your more blog for us. great content I am also a content writer recently i analysis about which provide all services related to as follows :
    1. best fashion accessories shops in udaipur
    2.best car accessories shops in udaipur
    3. best educational institution in udaipur
    4.best general stores in udaipur
    5.best sanitaryware dealers in udaipur
    Thanks for publishing regarding this. There’s the bunch associated with strong tech info on the actual internet. Your good deal of that info corrects here on your own site. I’m impressed — I try to maintain a couple blogs fairly current, however, it is challenging occasionally. I want to know more information deeply about it. If possible you can share your more blog for us.

    ReplyDelete
  8. I actually take care of such information a lot. I'm interested in this specific info for just a while. Appreciate it and also all the best .Led Lights Sydney

    ReplyDelete
  9. We offer university assignment help fulfilling the specific and precise needs of students. Poor quality of an assignment drastically affects the score sheet of a student. Here at Calltutors.com, the writers know how to proceed with an assignment writing task to make the end-result appreciable with world-class university assignment help online. We even welcome the feedback from the client’s end and try to sort out the shortcomings as soon as possible.
    https://www.calltutors.com/Articles/Writing-Assignment-for-University

    ReplyDelete
  10. Really nice post. It is very help for from validation .This code that every web developer helping for. Validating a form in all possible ways. very simple and effective code is that

    ReplyDelete
  11. This is exactly what I was looking for. Thanks for sharing this great article! That is very interesting Smile I love reading and I am always searching for informative information like this!

    ReplyDelete
  12. Lighting Warehouse Sydney Great blog, your blog looks amazing to reed & Thanks for posting

    ReplyDelete
  13. Commercial Led LightingIf you are going for most excellent contents like I do, simply pay a quick visit this web site all the time as it provides quality contents, thanks

    ReplyDelete
  14. Thank you,it is good material to read this post increases my knowledge. What a wonderful piece of information. Admiring the time and effort you put into your blog and detailed information you offer.
    hotmail email

    ReplyDelete
  15. f you want to make your house clean then you should know about drain cleaning.
    Do you know which are the best drain cleaners in the market?
    Then you should click the below link
    Read The Best Guide For Drain Cleaner reviews

    ReplyDelete
  16. Thank you for sharing such brilliant and beneficial social bookmarking sites. we are imparting decision associated with circulate QuickBooks Online Refund. If anyone dealing with problem related to QuickBooks Online Refund. So you can directly call us at our QuickBooks Online Support +1-877-249-9444.

    ReplyDelete
  17. Much thanks to you such a great amount for this post. It has the most enlightening well ordered guidelines I have perused. You can contact at this number +18555583999 to profit the administrations gave by Norton 360 Error 8506 and 421.

    ReplyDelete
  18. It gets into the root of the matter and offers a solution which may be implemented instantly. It thus handles to provide nearly instantaneous relief, and soothes the pain in minutes.

    ReplyDelete
  19. Awesome write-up. I’m a regular visitor of your site and appreciate you taking the time to maintain the excellent site.

    ReplyDelete

More Posts

FOLLOW