0

I have a form for which I should write validation logic.

Let's say there's an input field like this.

<div class="group1"><label>Product code</label> <input id="code" name=""code" type="text" class="control1"></div>

If I want to make it a required field how do I write the logic?

I don't have access to the CSS files. But there's an input like this which I can use which has a red outline.

<div class="group1 row has-error">
   <div><input type="text" class="control1"></div>
</div>

I have to give the code in JavaScript or jQuery.

3 Answers 3

1

Get an element and set true on required property.

const input1 = document.getElementById('code');
input1.required = true;

const input2 = document.querySelector('div.group1>div>input.control1');
input2.required = true;
<form>
  <div class="group1"><label>Product code</label>
    <input id="code" name="code" type="text" class="control1">
  </div>
  <div class="group1 row has-error">
    <div>
      <input type="text" class="control1">
    </div>
  </div>
  <input type="submit">
</form>

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

Comments

1

I think this is what you need

$("#formSubmit").submit(function(e) {
    e.preventDefault();
    var error_text = "<div class='text-danger error_val'>Cannot be empty</div>"
    var data = $("#formSubmit").serializeArray();
    var allInputs = $("#formSubmit input");
    for(var i=0; i<data.length; i++){
        if(data[i].value.length == 0){
            $(".group1").addClass('has-error');
            var errorDiv = $(allInputs)[i].closest('.has-error');
            $(error_text).insertAfter( errorDiv );
        }
    }
});
.has-error input{
 border: 1px solid #f00; 
}

.text-danger{
color:#f00;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formSubmit">
    <div class="group1 row">
        <label>Product code</label>
        <input id="code" name="code" type="text" class="control1" >
    </div>
    <button type="submit" id="submitButton">Save</button>
</form>

Comments

0

I know it`s too late but you can use these functions to make an input required/optional

function makeFieldRequired(element, elementName) {
  let jqueryObj = $(element);
  jqueryObj.attr('title', `${elementName} is required`);
  jqueryObj.attr('required', 'true');

  if (jqueryObj.closest("form").length)
    refreshFormValidtion(jqueryObj.closest("form"));
}

function makeFieldOptional(element) {
  let jqueryObj = $(element);
  jqueryObj.removeAttr('required');
  jqueryObj.removeAttr('title');

  if (jqueryObj.closest("form").length)
    refreshFormValidtion(jqueryObj.closest("form"));
}

function refreshFormValidtion(form) {
  $(form).removeData("validator").removeData("unobtrusiveValidation");
  $.validator.unobtrusive.parse($(form));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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.