thai-typing-validator
v1.0.0
Published
Validate Thai text input according to proper Thai typing rules - prevents invalid vowel and tone mark combinations
Maintainers
Readme
thai-typing-validator
A JavaScript/TypeScript library to validate Thai text input according to proper Thai typing rules. Prevents invalid combinations of vowels and tone marks.
Why This Package?
Thai script has specific rules about how vowel marks and tone marks can be combined. This library ensures that text input follows these rules:
- Lower vowels (ุ ู) cannot appear consecutively in the same character group
- Base upper vowels (ิ ี ึ) can have one additional mark stacked on top, but cannot stack another ิ ี ึ
- Other upper marks can only appear once per character
Perfect for Thai text inputs, content editors, and form validation!
Installation
npm install thai-typing-validatorUsage
Basic Validation (Real-time Input)
const { validateInput } = require('thai-typing-validator');
let currentText = 'ก';
// Try to add ุ
const result1 = validateInput('ุ', currentText);
console.log(result1); // { valid: true, message: '' }
currentText += 'ุ';
// Try to add another ุ (should fail)
const result2 = validateInput('ุ', currentText);
console.log(result2);
// { valid: false, message: 'Lower vowels (ุ ู) cannot be typed consecutively...' }HTML Input Integration
const { validateInput } = require('thai-typing-validator');
const input = document.getElementById('thaiInput');
let validText = '';
input.addEventListener('input', (e) => {
const newValue = e.target.value;
if (newValue.length > validText.length) {
const newChar = newValue[newValue.length - 1];
const validation = validateInput(newChar, validText);
if (!validation.valid) {
e.target.value = validText; // Reject invalid input
alert(validation.message);
return;
}
}
validText = newValue;
});Validate Entire Text
const { validateText } = require('thai-typing-validator');
const result = validateText('กุุ');
console.log(result);
// {
// valid: false,
// errors: [{
// position: 2,
// character: 'ุ',
// message: 'Lower vowels (ุ ู) cannot be typed consecutively...'
// }]
// }Sanitize Text
const { sanitizeText } = require('thai-typing-validator');
const clean = sanitizeText('กุุ้');
console.log(clean); // 'กุ้' (removes the invalid second ุ)TypeScript
import { validateInput, ValidationResult } from 'thai-typing-validator';
const result: ValidationResult = validateInput('ุ', 'ก');
if (!result.valid) {
console.error(result.message);
}API Reference
validateInput(newChar: string, currentText: string): ValidationResult
Validates if a new character can be added to the current text.
Returns:
{
valid: boolean;
message: string; // Error message if invalid
}validateText(text: string): TextValidationResult
Validates entire text and returns all errors found.
Returns:
{
valid: boolean;
errors: Array<{
position: number;
character: string;
message: string;
}>;
}sanitizeText(text: string): string
Removes invalid characters from text according to Thai typing rules.
getLastCharGroup(text: string): string
Gets the last character group (base character + all marks).
isThaiVowelMark(char: string): boolean
Checks if a character is a Thai vowel mark.
isToneMark(char: string): boolean
Checks if a character is a Thai tone mark.
categories
Exports character categories for advanced usage:
lowerVowels: ['ุ', 'ู']upperVowels: ['ิ', 'ี', 'ึ', 'ื', 'ั', '็']toneMarks: ['่', '้', '๊', '๋']specialMarks: ['์']baseUpperVowels: ['ิ', 'ี', 'ึ']allUpperMarks: All upper marks combined
Thai Typing Rules
This library enforces the following Thai typing rules:
No consecutive lower vowels: Characters like ุ and ู cannot appear twice in the same character group (e.g., กุุ is invalid)
Stacking rules for ิ ี ึ: These base upper vowels can have one additional mark on top (like tone marks), but you cannot stack another ิ ี or ึ on them
One upper mark per character: For characters without base upper vowels (ิ ี ึ), only one upper mark is allowed
Examples
Valid:
กุ(consonant + lower vowel)กิ้(consonant + base upper vowel + tone mark)กู่(consonant + lower vowel + tone mark)
Invalid:
กุุ(consecutive lower vowels)กิิ(stacking ิ on ิ)กั้่(two upper marks without base upper vowel)
Browser Support
Works in all modern browsers and Node.js 12+.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
Author
Sorracha Aiemmeesri
Made with ❤️ for the Thai language community
