TheSmile
TheSmile

Reputation: 360

Tag - How to set required for Tag it

Im using Jquery Tag it for my tag function and save value to DB.

Using jquery how to set myTags required and at least one tag else not able to proceed/save. Reason is <li> and <input> are auto generate.

HTML Code

<ul id="myTags" name="myTags"></ul>  

Jquery

$("#myTags").tagit({
    fieldName: "tags[]"
}); 

ScreenShot

enter image description here

Upvotes: 1

Views: 1001

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113345

TagIt library doesn't seem to provide an API for getting the list of tags. However, you can read the HTML and see if tags are there.

On form submit, count the tags and return false if data is not valid. Use .tagit("assignedTags") to get the list of tags:

var $tags = $("#myTags");
$tags.tagit({
    fieldName: "tags[]"
});

$("form").on("submit", function () {
    if (!$tags.tagit("assignedTags").length) {
        alert("Tags are mandatory");
        return false;
    }
    alert("Valid data");
});

JSFIDDLE

Upvotes: 2

stranded
stranded

Reputation: 322

You can use the assignedTags() method https://github.com/aehlke/tag-it/blob/master/README.markdown#assignedtags to check how many tags have been assigned and then move forward based on that. I guess for you something like this will work

if($("#myTags").tagit("assignedTags").length>0){
    //submit form
}
else{
    alert("please add at least one tag");
}

Upvotes: 0

Related Questions