Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  177] [ 2]  / answers: 1 / hits: 22208  / 11 Years ago, thu, june 27, 2013, 12:00:00

I'm working on a validation function for html input tags. I've the string of the value of the particular input and the string containing the allowed characters.



var allowed = 'abcdefghijklmnopqrstuvwxyz;
var value = element.value;


I'd like to write a function to determine if the value contains character only from the allowed string. I'm looking for a straight-forward and simple solution.
Any ideas?


More From » string

 Answers
83

Yes you can use regex



function alphanumeric(inputtxt)  
{
var letters = /^[a-z]+$/;
//if you want upper and numbers too
//letters = /^[A-Za-z0-9]+$/;
//if you only want some letters
// letters = /^[azertyuiop]+$/;
if(inputtxt.value.match(letters))
{
alert('Your registration number have accepted : you can try another');
document.form1.text1.focus();
return true;
}
else
{
alert('Please input alphanumeric characters only');
return false;
}
}

[#77377] Wednesday, June 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiyam

Total Points: 448
Total Questions: 96
Total Answers: 92

Location: Philippines
Member since Sat, Jul 11, 2020
4 Years ago
;