Skip to content
TheChatArchiveThe Chat Archive
Mistralmagistral-medium-2506· Mistral AIHugging Face LMArena Human Preference 140K

javascript solution to clear input type text from inserting anything else but letters…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Metallama-4-maverick-03-26-experimental’s answer.
  1. User

    javascript solution to clear input type text from inserting anything else but letters and digits

  2. Mistral Assistant

    Here's a concise solution to ensure that an input field of type text only accepts letters and digits:

    document.getElementById('myInput').addEventListener('input', function(event) {
        const input = event.target;
        const start = input.selectionStart;
        const end = input.selectionEnd;
        const value = input.value;
    
        let newValue = '';
        let validCharsBeforeCursor = 0;
    
        for (let i = 0; i < value.length; i++) {
            const char = value[i];
            if (char.match(/^[a-zA-Z0-9]$/)) {
                newValue += char;
                if (i < start) {
                    validCharsBeforeCursor++;
                }
            }
        }
    
        if (newValue !== value) {
            input.value = newValue;
            input.setSelectionRange(validCharsBeforeCursor, validCharsBeforeCursor);
        }
    });
    

    This code listens for the input event on the specified input field. When the event is triggered, it processes the current value of the input field, removing any characters that are not letters or digits. It also maintains the cursor position correctly by counting the number of valid characters before the original cursor position and setting the new cursor position to that count. This ensures a smooth user experience by preventing invalid characters from being entered and maintaining the cursor position appropriately.

  3. User

    clean up also spaces

  4. Mistral Assistant

    Here's a solution that allows letters, digits, and spaces, but cleans up spaces by trimming leading/trailing spaces and collapsing multiple spaces into one:

    document.getElementById('myInput').addEventListener('input', function(event) {
        const input = event.target;
        const start = input.selectionStart;
        const end = input.selectionEnd;
        const value = input.value;
    
        // Step 1: Remove all characters that are not letters, digits, or spaces
        let filteredValue = '';
        let cursorPosInFiltered = 0;
        let i, char;
    
        for (i = 0; i < value.length; i++) {
            char = value[i];
            if (char.match(/^[a-zA-Z0-9 ]$/)) {  // Allow letters, digits, and spaces
                filteredValue += char;
                if (i < start) {
                    cursorPosInFiltered++;
                }
            }
        }
    
        // Step 2: Clean up spaces in filteredValue and track cursor position
        // First, trim leading and trailing spaces
        let trimmedValue = filteredValue.trim();
        let cursorPosInTrimmed = cursorPosInFiltered;
    
        // Adjust cursor position after trimming
        const leadingSpacesRemoved = filteredValue.length - filteredValue.trimLeft().length;
        if (cursorPosInFiltered <= leadingSpacesRemoved) {
            cursorPosInTrimmed = 0;
        } else {
            cursorPosInTrimmed = cursorPosInFiltered - leadingSpacesRemoved;
            // If cursor was in trailing spaces, adjust
            const trailingSpacesRemoved = filteredValue.length - filteredValue.trimRight().length - leadingSpacesRemoved;
            if (cursorPosInFiltered > filteredValue.length - trailingSpacesRemoved) {
                cursorPosInTrimmed = trimmedValue.length;
            }
        }
    
        // Now, collapse multiple spaces into one
        let collapsedValue = '';
        let cursorPosInCollapsed = 0;
        let inSpaceSequence = false;
    
        for (i = 0; i < trimmedValue.length; i++) {
            char = trimmedValue[i];
            if (char === ' ') {
                if (!inSpaceSequence) {
                    collapsedValue += char;
                    if (i < cursorPosInTrimmed) {
                        cursorPosInCollapsed++;
                    }
                    inSpaceSequence = true;
                }
            } else {
                collapsedValue += char;
                if (i < cursorPosInTrimmed) {
                    cursorPosInCollapsed++;
                }
                inSpaceSequence = false;
            }
        }
    
        // Now, set the new value and cursor position
        if (collapsedValue !== value) {
            input.value = collapsedValue;
            // Ensure cursorPosInCollapsed is within bounds
            cursorPosInCollapsed = Math.min(cursorPosInCollapsed, collapsedValue.length);
            input.setSelectionRange(cursorPosInCollapsed, cursorPosInCollapsed);
        }
    });
    

    This code ensures that only letters, digits, and spaces are allowed, and it cleans up spaces by trimming leading/trailing spaces and collapsing multiple spaces into one. The cursor position is carefully tracked and adjusted throughout these transformations to provide a good user experience.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.