0

I'm currently using code like this:

    var alphaExp = /^[a-zA-Z]+$/;
    if (f_name.value.length > 1 && f_name.value.match(alphaExp)) {
        // success
    }
    else {
        document.getElementById("f_name_mark").innerText = "<img src='images/icons/cross.png' class='mark'>";
        // fail
    }

However, alphaExp only accounts for upper- and lower-case English characters. How can I allow for foreign letters (é, å, ü etc) and spaces/hyphens?

Sebastian
  • 3,548
  • 18
  • 60
  • 95
  • Perhaps you might find this question interesting: http://stackoverflow.com/questions/990904/javascript-remove-accents-in-strings. Use that code to remove accents, then just check with your current regexp (and use `[a-zA-Z -]`). – pimvdb Aug 02 '11 at 17:08
  • I currently have /^[a-zA-Z\sàèìòùáéíóúäëïöüñãõåæøâêîôû-]+$/ but it's not working. What have I done wrong? – Sebastian Aug 02 '11 at 17:16
  • A problem could be the fact you're using match instead of test... Use `alphaExp.test(f_name.value)` instead – user278064 Aug 02 '11 at 17:30

2 Answers2

1

You can try following regex:

/^[a-z- \xC0-\xFF]+$/i

Sample

JSFiddle

function validate() {
  var regex = /^[a-z- \xC0-\xFF]+$/i;
  var value = document.getElementById("txt").value;
  document.getElementById("result").innerHTML = regex.test(value) ? "valid": "incorrect"
}
<input type="text" id="txt">
<button onclick="validate()">Validate</button>
<p id="result"></p>

Encoding Reference

Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

Insert \s to allow spaces. Add the others letters or symbols you want match.

var alphaexp = /^[a-zA-Z\séåü]+$/

user278064
  • 9,982
  • 1
  • 33
  • 46