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



39 comments:

  1. 'I'm highly impressed by the piece of thoughts you have shared on this portal. all the best
    connect us on Assignment Help can shed your burden of assignments with a return of qualitative assignments.
    Online Assignment Help

    ReplyDelete
  2. Thanks so much for the blog post its very informative and I commend you for that. I will have to bookmark this site and show my friends. I also found below links helpful maybe you can have a look as you can get a assignment help online or an essay help online. You can check our sample research questions, essay questions as well as essay answers for the papers we have done before.

    ReplyDelete
  3. I am a freelancer and have been helping a lot of students in providing assistance to students who frequently gets worried about their assessment tasks and went online to search for assignment help uk terms.
    We at My Assignment Services have potentially recognized the need way back and started what we are an enlarged version of then. With many assignment helper companies in the market, no one has quite able to match the standard that we follow. We give utmost priority to students by strictly adhering to the assignment requirements as well as marking rubrics to score high grades. Our Essay Writer is well-proficient in not only comprehending your assessment tasks but also to give an expert finish to it.
    Provided a well-written assignment suitable to your needs, your search for assignment help will now end with My Assignment Services. You just have to visit our website for more information.

    ReplyDelete
  4. Assignment Help connects students with professional academic writers in an effective manner. Use assignment help services if you want professional assignment writing for your project submission being a student of UK universities.
    Assignment Help Online
    Online Assignment Help
    Assignment Help Online Services
    Assignment Helper
    Assignment Assistance
    Assignment Help Experts
    Online Assignment Help Services

    ReplyDelete
  5. Thanks for sharing the PHP JavaScript here, this script would be helpful to develop My website.

    ReplyDelete
  6. Very informative and well written post! Quite interesting and nice topic chosen for the post. My Assignment Help Au has been a pioneer within the Best Assignment Help on-line services and within the Australia and different countries ceaselessly for over Seven years. Make an order now for professional work, delivered on time and pure. You can email us at cs@Myassignmenthelpau.Com or Phone Number: +61-2-8005-8227.

    ReplyDelete
  7. Use Finance Assignment Help if you don’t find easy to solve your calculation part. Don’t waste your time by solving a complexes question under more than enough time; chose the best writer to get point-to-point information.
    Finance Homework Help
    Finance Assignment
    Personal Finance Assignments
    Finance Help Online
    What is peel writing

    ReplyDelete
  8. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. If you're looking for CDR Report Help. You can email us at cs@needcdrreport.com or Phone Number: +61-2-8005-8227

    ReplyDelete
  9. Nice post, I like this types of post, Are you searching the quality food for your pet in UK? Do you know raw food is perfect food than traditional food? Do you want to know more information regarding dog food? You must visit RawDogsFood at once. This shop offers different recipe of foods for your puppy and Blue dog. This shop has recognized by UK Government.
    https://www.minds.com/harrylindsay/blog/which-is-the-best-blue-dog-food-raw-or-commercial-food-1077169135713935360

    ReplyDelete
  10. Nice post, this post inspire me to write something, this is a best post for me. When I was write post, there would be great possibility for errors. But when I read this post this is like a awesome post for me. Thanks
    https://jsttocute.wordpress.com/2020/02/19/trendy-shop-for-kids-perfect-baby-shop-online-wholesale/
    https://maidmarionsclean.wordpress.com/2020/02/18/professional-builders-cleaning-services-birmingham-uk/

    ReplyDelete
  11. Looking for the best Web Design Services Birmingham? Sds software is the right choice for you. Before select us see our responsive designed websites, our experience, Reviews and our record to complete assignment

    ReplyDelete
  12. Webguruawards is a company which provides Web Awards for creative web designer or developer. If you have website and want to be famous internationally and want to get web awards, you must visit Webguruawards at once.

    ReplyDelete
  13. As I can see, everyone is suggesting to do your homework yourself which is absolutely fine. Why homework are given so that the things which are taught in classes can be revised. And it is easy for us to understand that topic better. But there is also a point which can not be denied. We are not taught only one subject in colleges but there are subjects in bulk. And we are given assignments or homework for every single subjects. Sometimes we miss some lectures due to some reasons. And if it is mandatory to submit the homework on time. In that case, what to do? When we were younger, didn't we take the help of the elders or the help of the teachers in doing the homework? So, what's wrong now to take help from someone else? The point is that it shouldn't be in our habit to take someone's else again and again. We are human beings, and there is nothing wrong in taking homework help. 

    If you are in need and looking for "can someone do my homework for me", just go with the online assistance of My Assignment help. Here you can have all kinds of academic help from expert writers when you desperately need someone's help. 

    ReplyDelete
  14. Myassignmenthelp is an incomparable online assignment writing service delivering excellent Science assignment help service. Through a team of over 3000 subject experts we offer individual attention to every student making the assignment help experience completely unique and stress-free for students aiming to score high in every assignment. We even offer you free samples on our website that can be used as a quality reference to write a fresh piece of work and improve your grades.

    ReplyDelete
  15. ABC Assignment Help is the most recognized and preferred one stop solution for students to get professional help in academic assignments, essays, research papers, reports and coursework. Our writers are experienced and competent to help students score outstanding grades and remarkable knowledge about the concerned subject.

    Read More - mechanical engineering assignment help

    ReplyDelete
  16. ABC Assignment Help is an incomparable online Marketing assignment help company delivering excellent academic assignments, essays, coursework and reports. Through a team of over 3000 subject experts we ensure individual attention to every student making the assignment help experience completely personalized in nature. With our round the clock services, you can be assured of high grades every time.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. If you are looking for all the hot Islamabad Escorts girls and also the special class Escorts to satisfy all your needs while looking for the best fun of your life +92-3000078885, then the VIP Escorts in Islamabad are very accessible for you. We have the best Escorts Service in Islamabad. Highly mature and can give you a pleasant joy. https://vk.com/islamabadescorts

    ReplyDelete
  19. How many times have you ever written ‘online coursework help.’ on Google to seek out the reliable writing services in US? you get the Assignment Help then you have contact our expert team. The support team at GetAssignmentHelpOnline is online 24*7. you'll get in touch with them over the phone or through chat for fast response. you'll also send us an email stating your queries. they're going to go back to to you within a matter of minutes.Visit:
    online coursework help
    online coursework writing
    coursework assignment help
    online coursework service
    coursework help online
    coursework writing service

    ReplyDelete
  20. I m really looking forward to read more, Your site is extremely helpful for us, this is often one among the awesome post i got the simplest information through your site and Visit also this site, check it out
    Call Girls in Lahore
    Lahore Call Girls
    Independent Call Girls in Lahore
    Cheap Lahore Call Girls
    Islamabad Escorts
    Escorts in Islamabad
    Escorts Service in Islamabad
    Islamabad Escorts Service

    ReplyDelete
  21. Use Assignment Help if you have doubts about the assigned topic and can't complete your papers according to your professor's demand. Follow the necessary steps to access the best services from the Australian service provider and finish your work without any issue.
    My Assignment Help
    Assignment Help Online
    Online Assignment Help
    Assignment Help Australia

    ReplyDelete
  22. Your article really helped me understand this topic. I am glad that I stumbled upon this site. Actually I have visited other sites on this same topic as well, but yours is truly amazing. Anyone in need of Assignment Help can get my help. My company is providing some support in the assignment. I know writing assignments are a nightmare. And when your deadline is close, then it gets hell lot tougher to sleep well at night. And that’s why my company is in this service.
    Assignment Help Online
    Best Assignment Help
    Assignment Helper
    Assignment Help In USA

    ReplyDelete
  23. Very nice!!! This is really good blog information thanks for sharing.

    Language Tutors UK

    ReplyDelete
  24. You had me engaged by reading and you know what? I want to read more about that. That's what's used as an amusing post.
    read more:- Marketing Assignment Help

    ReplyDelete
  25. Amazing post! This blog gives the amazing ideas to explore Nepal tourism. Tourists can take ideas to get the best Nepal Tour Packages from traveling agency. Thanks for sharing this amazing blog.
    Nepal Tour Package
    Nepal Tour

    ReplyDelete
  26. This is really a great article, I am happy to spend a quality time here I found adult Escorts Sites for those who want ideal service and unique fun with young Call Girls in Lahore, if you want to make a wonderful date with hot looking women’s, then just visit and call them.
    Escorts in Bahria Town
    Call Girls in Bahria Town
    Escorts in Wagah Town
    Call Girls in Wagah Town
    Lahore Escorts
    Escorts in Lahore
    Call Girls in Lahore
    Lahore Call Girls
    Escorts Service in Lahore
    Lahore Escorts Service
    Model Escorts in Lahore
    Model Call Girls in Lahore

    ReplyDelete
  27. The foremost reason for us leading the Australian market is our writers’ approach towards their responsibility. Our online assignment writers immensely focus on each assignment they bid for. Our professional writers are among those who exactly know the importance of students to meet their desired requirements. Keeping your requirements and expectations in mind, our writers outperform each order, leaving no leaf un-turn.

    ReplyDelete

More Posts

FOLLOW