Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a 3 input fields that get checked and button becomes enabled. However i want to flag custom error messaging if criteria is not valid.

What I have tried:

CSS
 .textinput {
    
            border: 1px solid #000;
            border-radius: 0;
            box-sizing: border-box;
            color: #6c6c6c;
            height: auto;
            margin: 0px auto 15px auto;
            padding: 4px 0 5px 10px;
            width: 100%;
            font-family: Apercuregular,sans-serif;
            font-weight: normal;
            letter-spacing: .02em;
            font-size: 16px;
            display: block;
             width: 300px;
  }
  
   .form-button{
    background-color: #000;
    color: #fff;
    display: block;
    letter-spacing: .02em;
    font-family: Apercuregular,sans-serif;
    font-size: 13px;
    text-align: center;
    text-decoration: none;
    text-transform: uppercase;
    padding: 1.2em 2em 1.2em 2em;
    width: 275px;
    margin: 25px auto 25px auto;
    border: 1px solid #000;
    outline: none;
  }
  .form-button[disabled],
    .form-button[disabled]:hover {
        background-color: red;
        color: #fff;
        border: 0 none;
    }

  
  
  .fieldLabel {
    display: block;
    font-size: 13px;
    font-family: Apercuregular,sans-serif;
    letter-spacing: .02em;
    text-align: left;
    width: 100%;
    line-height: 17px;
  }
  
   input[type="checkbox"]{
  display: none;
}

 
input[type="email"]  {
   -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
  }


HTML
<div id="container_COLUMN1">
                    <input type="text" name="First Name" id="control_COLUMN1" label="First Name" class="textinput mandatory" placeholder="First name*" required labeltext="First name*" maxlength="50" mandatory="true" onblur="valid();" />
                </div>
                
                <div id="container_COLUMN2">
                    <input type="text" name="Last Name" id="control_COLUMN2" label="Last Name" class="textinput mandatory" placeholder="Last name*" required labeltext="Last name*" maxlength="50" mandatory="true" onblur="valid();" />
                </div>
                
                <div id="container_Email">
                    <input type="text" name="Email" id="control_COLUMN3" label="Email" class="textinput mandatory" placeholder="Email*" required labeltext="Email*" maxlength="50" mandatory="true" onblur="valid();" />
                </div>
              
             
                
                <div>
                  <button type="submit" disabled class="defaultText form-button" id="submitBtn" value="Enter Now">Enter Now</button>
                </div>


JavaScript
                function valid(){
  var fn = document.getElementById('control_COLUMN1').value;
  var ln = document.getElementById('control_COLUMN2').value;
  var em = document.getElementById('control_COLUMN3').value;
  var name = /^[A-Z]?[a-z]*(\s[A-Z][a-z]*)*$/;
  var email = /^[a-z0-9]+\@[a-z0-9]+\.[a-z]+/;
  if(email.test(em) && name.test(fn) && name.test(ln))
    document.getElementById('submitBtn').disabled=false;
  else
    document.getElementById('submitBtn').disabled=true; 
}
Posted
Updated 2-Mar-17 23:32pm
Comments
CHill60 27-Feb-17 10:45am    
So what is wrong with your code?
Member 13026670 27-Feb-17 12:00pm    
nothing wrong with current version but I am not sure how to apply error messaging?

Start by using the correct input type, and adding the pattern attribute[^] to your name inputs, so that the browser will validate them:
HTML
<input type="text" name="First Name" id="control_COLUMN1" label="First Name" class="textinput mandatory" placeholder="First name*" required maxlength="50" pattern="[A-Z]?[a-z]*(\s[A-Z][a-z]*)*" />
...
<input type="text" name="Last Name" id="control_COLUMN2" label="Last Name" class="textinput mandatory" placeholder="Last name*" required maxlength="50" pattern="[A-Z]?[a-z]*(\s[A-Z][a-z]*)*" />
...
<input type="email" name="Email" id="control_COLUMN3" label="Email" class="textinput mandatory" placeholder="Email*" required maxlength="50" />

Then, add the jQuery Validation Plugin[^] to your page and validate the form.
HTML
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="jquery.validate.js"></script>
<script>
$(function(){
    $("form").validate();
});
</script>
 
Share this answer
 
You can add one empty div tag top of the form like below:

<div id="error"></div>


Then update the else part of the javascript like below:

 if(email.test(em) && name.test(fn) && name.test(ln))
    document.getElementById('submitBtn').disabled=false;
  else
    document.getElementById('error').innerText = "Please fill all data";
    document.getElementById('submitBtn').disabled=true;

}


I hope the above will work.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900