Reputation: 7563
I have a textfield on a page which allows people to enter their name. This then gets included in a URL specially made for them e.g. mysite.net/johnnyfive
I don't want people to use any special characters in their name, including characters from non-English alphabets such as Arabic or Chinese.
How would I validate for the English alphabet in ColdFusion? I came up with this for English names #REReplace(LCase(ARGUMENTS.Name), '[^a-z0-9]', '', 'ALL')#
to ensure only characters A-Z and numbers are allowed. But if someone has entered in something like نازنین
then I want to it reject it outright rather than trying to replace it.
I'd also like to do this on the jQuery side and bind it to the textfield so that it throws an error when someone tries to enter in a non-English character. I suppose even diacritic characters such as á
should not be allowed.
I am using ColdFusion 10, jQuery 1.9, and jQuery Validate
Upvotes: 2
Views: 4592
Reputation: 4475
You could use the DeAccent UDF (java):
http://www.cflib.org/udf/deAccent
<cfscript>
function deAccent(str){
var Normalizer = createObject("java","java.text.Normalizer");
var NormalizerForm = createObject("java","java.text.Normalizer$Form");
var normalizedString = Normalizer.normalize(str, createObject("java","java.text.Normalizer$Form").NFD);
var pattern = createObject("java","java.util.regex.Pattern").compile("\p{InCombiningDiacriticalMarks}+");
return pattern.matcher(normalizedString).replaceAll("");
}
</cfscript>
and then perform a string compare:
if (originalString NEQ deAccent(originalString)){
writeOutput("Invalid String");
}
If you wanted to validate using jQuery, it could be performed with an ajax request to the ColdFusion server. You should probably do this to ensure that the string is unique as you stated that you plan on creating sub-directories/virtual paths.
Upvotes: 2
Reputation: 26667
You can use the same regex to validate the fields. Add anchors around the regex to ensure that nothing is presceded or followed by the string matched by the regex
^[^a-z0-9]$
^
anchors the regex at the start of the string.
$
anchors regex at the end of the string.
Example : http://regex101.com/r/tJ7tJ9/1
You can use the match function to ensure that the string matches the regex
Example
var str = "نازنین";
if(str.match(/^[a-z0-9]+$/gi))
{
//Do something
}
else
{
// Do something else
}
Upvotes: 6