Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6249957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:20:01+00:00 2026-05-24T13:20:01+00:00

I am creating a form that allows you to sign up for alerts on

  • 0

I am creating a form that allows you to sign up for alerts on our website, by allowing a user to input the keywords into a form.

I have setup the validation, although the jquery I have used seems somewhat convoluted even to me, it works!

I am now trying to setup the form so that when you input a keyword, it checks to see if you have already put in that keyword.

e.g. I’ve added “cloud” to my list, and I accidentally try to add it again, the form should alert me and not allow me to add it to my list.

I think the solution would be to store the keyword in a “KeywordList” array, then cycle through using a for each loop like in PHP:

foreach (KeywordList as KeywordListArrayElement){
         if(KwValue == KeywordListArrayElement){
           alert ("The keyword '+KwValue+' has already been added");
           return false;
         }
}

But I don’t know how to store the Keyword in the array or fetch it from it

here is my html:

    <form action="" method="get" name="Test Form" target="_self" id="alerts_form" dir="ltr" lang="en">
        <fieldset id="alerts_options">
            <label for="email_address">Email: </label>
            <input type="text" id="email_address" name="email_address" tabindex="1" autofocus="autofocus" autocomplete="on" required="required" value="" /><div class="email-alert form_val" style="display: none;">Please enter a valid email address</div><br />
            <label for="keywords">Keyword(s): </label>
            <input type="text" id="keywords" name="keywords[]" tabindex="2" autocomplete="on" value="" />
            <button id="addKeyword" name="addKeyword" value="">+</button><br />
            <div id="KeywordList"></div>
            <div class="keyword-alert form_val" style="display: none;">Please input at least one keyword</div>
            <label for="frequency">Frequency: </label>
            <select tabindex="3" id="frequency" name="frequency"><br />
            <optgroup>
            <option selected="selected" value=""></option>
            <option value="1">Daily</option>
            <option value="7">Weekly</option>
            <option value="30">Monthly</option>
            </optgroup>
            </select><div class="frequency-alert form_val" style="display: none;">Please make a selection</div><br />
            <input type="hidden" id="Step" name="Step" value="2" />
            <div class="clear"></div>
            <button class="alert_button" type="submit" id="submit" name="UpdateAlertOption" value="Unconfirmed">Submit</button><br />
            <button class="alert_button" type="submit" name="UpdateAlertOption" value="Delete">Delete</button><br />
            <button class="alert_button" type="submit" name="UpdateAlertOption" value="Delete All">DeleteAll</button>
            <input type="hidden" value="" name="" class="KwdBox" />
        </fieldset>
    </form>

Here is my JQuery:

<script type='text/javascript'>
jQuery(document).ready(function($){

    $("#addKeyword").click(function(event) {
    event.preventDefault();
    var KwValue = $("#keywords").val();
    if($("#keywords").val()==""){
        alert('Please input a keyword');
        }

        else
        {
            var squareBrackets = "[]";

                $("#keywords").css('background-color', '#FFFFFF');
                $("#alerts_options .keyword-alert").hide();
            $('<input type="text" class="KwdBox" name="keywords'+squareBrackets+'" value="'+KwValue+'" tabindex="2" autocomplete="on" required="required"/>')
            .appendTo('#KeywordList');
            $("#keywords").val("");
            $(".KwdBox").click(function(event){
                event.preventDefault();
                $(this).remove();

            });
        }
    });

    $("#alerts_options .email-alert").hide();
    $("#alerts_options .keyword-alert").hide();
    $("#alerts_options .frequency-alert").hide();

    $("#submit").click(function(){


        var frequency = 1;
        if($("[name=frequency]").val()==""){
        frequency = 0;
        }else{
        frequency = 1;
        }
        var keyword = 1;
        if($("#keywords").val()=="") {
        keyword = 0;
        }
        if($(".KwdBox").val() >""){
        keyword = 1;
        }
        var email = 1;
        if(!checkEmail($("[name=email_address]").val())){
        email = 0;
        }else{
        email = 1;
        }
        //frequency empty, email empty, kw empty
        if(frequency == 0 && email == 0 && keyword == 0){
            $("#keywords").css('background-color', 'rgb(255, 232, 232)');
            $("[name=email_address]").css('background-color', 'rgb(255, 232, 232)');
            $("[name=frequency]").css('background-color', 'rgb(255, 232, 232)');        
            $("#alerts_options .form_val").show();
            return false;
        }
        else{
            $("#alerts_options .form_val").hide();
        } //frequency empty, email on, kw empty
        if(frequency == 0 && email > 0 && keyword == 0){
            $("#keywords").css('background-color', 'rgb(255, 232, 232)');
            $("[name=email_address]").css('background-color', '#FFFFFF');
            $("[name=frequency]").css('background-color', 'rgb(255, 232, 232)');    
            $("#alerts_options .email-alert").hide();
            $("#alerts_options .keyword-alert").show();
            $("#alerts_options .frequency-alert").show();
            return false;
        }//frequency empty, email on, kw on
        if(frequency == 0 && email > 0 && keyword > 0){
            $("#keywords").css('background-color', '#FFFFFF');
            $("[name=email_address]").css('background-color', '#FFFFFF');
            $("[name=frequency]").css('background-color', 'rgb(255, 232, 232)');
            $("#alerts_options .email-alert").hide();
            $("#alerts_options .keyword-alert").hide();
            $("#alerts_options .frequency-alert").show();
            return false;
        }//frequency on, email on, kw empty
        if(frequency > 0 && email > 0 && keyword == 0){
            $("#keywords").css('background-color', 'rgb(255, 232, 232)');
            $("[name=email_address]").css('background-color', '#FFFFFF');
            $("[name=frequency]").css('background-color', '#FFFFFF');
            $("#alerts_options .email-alert").hide();
            $("#alerts_options .keyword-alert").show();
            $("#alerts_options .frequency-alert").hide();
            return false;
        }
        if(frequency > 0 && email == 0 && keyword > 0){
            $("#keywords").css('background-color', '#FFFFFF');
            $("[name=email_address]").css('background-color', 'rgb(255, 232, 232)');
            $("[name=frequency]").css('background-color', '#FFFFFF');
            $("#alerts_options .email-alert").show();
            $("#alerts_options .keyword-alert").hide();
            $("#alerts_options .frequency-alert").hide();
            return false;
        }
        if(frequency == 0 && email == 0 && keyword > 0){
            $("#keywords").css('background-color', '#FFFFFF');
            $("[name=email_address]").css('background-color', 'rgb(255, 232, 232)');
            $("[name=frequency]").css('background-color', 'rgb(255, 232, 232)');
            $("#alerts_options .email-alert").show();
            $("#alerts_options .keyword-alert").hide();
            $("#alerts_options .frequency-alert").show();
            return false;
        }
            if(frequency > 0 && email == 0 && keyword == 0){
            $("#keywords").css('background-color', 'rgb(255, 232, 232)');
            $("[name=email_address]").css('background-color', 'rgb(255, 232, 232)');
            $("[name=frequency]").css('background-color', '#FFFFFF');
            $("#alerts_options .email-alert").show();
            $("#alerts_options .keyword-alert").show();
            $("#alerts_options .frequency-alert").hide();
            return false;
        }

    });
});
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:20:02+00:00Added an answer on May 24, 2026 at 1:20 pm

    You don’t have to use an array. Use a map/associative-array instead. You can do something like this:

    var keywords = {};
    

    Then if your keyword is in the variable keyword, you can do:

    keywords[keyword] = true;
    

    So if you had the keyword cloud, what you basically have is:

    keywords['cloud'] = true;
    

    To check for its existence, all you have to do is:

    if(keywords[keyword]) {
      ...
    }
    

    Now to add your keywords to the list, you can do something like this:

    var KwValue = $("#keywords").val();
    
    var keywordArray = KwValue.split(/\s*,\s*/); //assuming that your keywords are separated by commas:
    
    for(var i = 0; i < keywordArray.length; i++) {
        var keyword = keywordArray[i];
    
        if(!keywords[keyword]) {
           keywords[keyword] = true;
           ...
        }
    }
    

    This is O(n), but it is better than the O(n2) you would get with an array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an add form that allows me to add the specific field into
I'm creating a Drupal form with a datetime-based javascript popup calendar input that allows
I'm creating a form that allows users to upload 11 images, i have 11
I'm creating an application (Windows Form) that allows the user to take a screenshot
I am creating an ASP.NET application that allows the user to add form elements
I'm creating a form that allows the user to search a database for different
I'm creating a form. As of right now I have simple MAILTO form that
I'm creating a user form that requires the functionality for people to come back
I am creating a form designer and have a toolbox that contains images of
I've creating one ASP web application, in that application one form have to update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.