Remove text from textbox after fifth comma

Discussion in 'jQuery' started by mooiman, Nov 27, 2014.

  1. #1
    I've got a text input field on my website with id 'fieldone', like this:

    <input type="text" name="fieldone" id="fieldone" value="">
    Code (markup):
    In this field, people may enter 5 words maximum seperated by comma's.

    So I build in a check if there are max. 5 words seperated by comma's, this check is triggered everytime a comma is entered. The check works great, but it still allows people to enter some words after the 5th comma. Is there a way to remove everything in the 'fieldone' textbox after the 5th comma?
     
    mooiman, Nov 27, 2014 IP
  2. kutchbhi

    kutchbhi Active Member

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    70
    #2
    If I understand you correctly, you can
    count the number of commas (triggered when a comma is entered)
    make sure the number of commas doesn't go over 4
    If a fifth comma is entered simply return false (in the keydown callback).
     
    kutchbhi, Nov 27, 2014 IP