You may want to take a look at regular expressions. They can be used in A LOT of programming languages, including javascript (see example below) and php (
preg_match(pattern, target) )
<html>
<head>
</head>
<script type="text/javascript">
var result;
var input;
function init(){
result = document.getElementById('result');
input = document.getElementById('input');
}
function validateLettersOnly(){
var regex = /^[ ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ]+$/;
test(regex);
}
function validateNumbersOnly(){
var regex = /^[1234567890]+$/;
test(regex);
}
function validateNumbersOnly2(){
var regex = /^\d+$/;
test(regex);
}
function validateSpecialCharacters(){
var regex = /^[_\-#"\/\.,°]+$/
test(regex);
}
function test(regex){
if( regex.test(input.value) ){
result.innerHTML = "Correct";
} else {
result.innerHTML = "Incorrect";
}
}
</script>
<body onload="init()">
<input id="input" type="text"/>
<div id="result"></div>
<input type="button" value="validateLettersOnly" onclick="validateLettersOnly()"/>
<input type="button" value="validateNumbersOnly" onclick="validateNumbersOnly()"/>
<input type="button" value="validateNumbersOnly2" onclick="validateNumbersOnly2()"/>
<input type="button" value="validateSpecialCharacters" onclick="validateSpecialCharacters()"/>
</body>
</html>
In javascrpit you define a regex between 2 forward slashes /pattern/
The pattern of /^[1234567890]+$/
is actually ^[1234567890]+$
^ at the beginning and $ at the end mean the whole line must match this regex.
[1234567890] = Any character within these brackets can be used
'+' = 1 or more times. (so empty string is invalid).
"\d" stands for any digit in regexes.
Certain special characters must be escaped in regexes, like the '-' and '/'. This is done by prepending a backwards slash.
So
_
-#"
/\.,°
became
_
\-#"
\/\.,°
The '/' because otherwise javascript thinks the regex is finished.
The '-' because that's a symbol used for range. If you wanted to test a value if it contains letters from the alphabet you would do (normal alphabet, not the spanish/italian/portugese you use :) )
^[a-ZA-Z]+$
(A quick google for "regex" or "regular expression" propably gives you all the info you need)