0

I am trying to display error message below the "edit-first-name" field. But It is not displaying error message. Could someone please tell where I am going wrong?

<HTML>
    <div class= "col-xs-12 col-sm-12 col-md-6 highlight" > 
    <form id= "editform" class= "userform" method= "post"> 
    <label class= "bold" for= "first-name" > First Name: </label> 
    <input id= "edit-first-name" type= "text" name= "first-name" 
     maxlength= "128" class= "required"
    data-msg= "Please provide First Name" placeholder= "First Name" value= ""> 
    <div class="row md-gray-bg margin-md-0">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <input id="cbot-save-ticket" tabindex="100" type="submit"
               class="button pull-right ignore"
               value="Save Ticket"/>
    </div>
</HTML>

Javascript:
<script type="text/javascript">
    document.getElementById("editform").addEventListener('submit',
    function  (event) {
    var focusSet = false;
    var fname = document.getElementById("edit-first-name");
    if(!(fname.value.length))
     {
       fname.after().html("<div class='msg-error' style='color:red;
       margin-bottom: 20px;'>Please enter email</div>");
        event.preventDefault();
        fname.focus();

     }
     });
0

1 Answer 1

1

after() is a jquery method. The after() method inserts specified content after the selected elements, but you need to include jquery for using it. You can simply put the error message div hidden, and when the validation fails, display the error message div. Below is the code snippet for the same.

document.getElementById("editform").addEventListener('submit',
    function  (event) {
    var focusSet = false;
    var fname = document.getElementById("edit-first-name");
    if(!(fname.value.length))
     {
        document.getElementById('msg-error').style.display = 'block';
        event.preventDefault();
        fname.focus();

     }
     });
<div class= "col-xs-12 col-sm-12 col-md-6 highlight" > 
    <form id= "editform" class= "userform" method= "post"> 
    <label class= "bold" for= "first-name" > First Name: </label> 
    <input id= "edit-first-name" type= "text" name= "first-name" 
     maxlength= "128" class= "required"
    data-msg= "Please provide First Name" placeholder= "First Name" value= ""> 
      <div class='msg-error' id='msg-error' style='color:red;margin-bottom: 20px;display:none;'>Please enter email</div>
    <div class="row md-gray-bg margin-md-0">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <input id="cbot-save-ticket" tabindex="100" type="submit"
               class="button pull-right ignore"
               value="Save Ticket"/>
    </div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.