-1

I have created a function using reg-ex to validate a field in JavaScript. My function is

//FIRST NAME VALIDATION
var strFilter = /^[A-Za-z]*$/;
var fname = document.getElementById('fname');
fname = fname.value.replace(/^\s+|\s+$/g,'');

if ( ( !strFilter.test(fname.value) ) || fname == '') { 
    alert("Please enter a valid first name\r\n (only characters)");
    document.getElementById('fname').style.background = '#DFE32D';
    document.getElementById('fname').focus();
    document.getElementById('fname').value = '';
    return false;
}

Issue with the function is, it is taking stackoverflow234232 also as true. I want to constrain it for character only.

hugomg
  • 68,213
  • 24
  • 160
  • 246
Rahul Singh
  • 1,614
  • 6
  • 22
  • 39
  • 2
    `/^[A-Za-z]*$/.test("stackoverflow234232")` yields `false` for me. – pimvdb Nov 23 '11 at 18:52
  • 2
    From [tchrist's post](http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129): "Code that believes someone’s name can only contain certain characters is stupid, offensive, and wrong." – NullUserException Nov 23 '11 at 18:53
  • /^[A-Za-z]*$/.test("stackoverflow234232") returns false actually. – mamoo Nov 23 '11 at 18:55
  • 1
    Agree with both comments, works for me but what happens when "Renée" tries to enter her name. – Godwin Nov 23 '11 at 18:55
  • If it would returns false I won't put the question here.... I can show you live example.... `http://www.oxfordmontessori.com/register-user.php`. Visit the link enter first name... this is js for that field... – Rahul Singh Nov 23 '11 at 18:58
  • @Godwin Or Daniel O'Higgins. Or Omar al-Bashir. etc. etc. – NullUserException Nov 23 '11 at 18:59

2 Answers2

3

I think your problem is on this line -

if ((!strFilter.test(fname.value)) || fname == '') { 

it should be -

if ((!strFilter.test(fname) ) || fname == '') { 

fname is a string variable by the time you get to the if statement, so fname.value is returning undefined and the call to test is not working.

ipr101
  • 24,096
  • 8
  • 59
  • 61
  • Whats the difference between both. Because I think in both cases it takes value.... – Rahul Singh Nov 23 '11 at 19:01
  • 2
    @Rahul No, it does not. You make `fname` a string on the previous line. You cannot access the original HTMLElement after that. – J. K. Nov 23 '11 at 19:04
  • 2
    @Rahul If you have `var hello = 'hello';` then `console.log(hello);` will return 'hello' but `console.log(hello.value);` will return `undefined` as hello has no `value` property. Even though `fname` starts as a textbox object it gets converted to a string on this line `fname = fname.value.replace(/^\s+|\s+$/g,'');` – ipr101 Nov 23 '11 at 19:06
0

Try this instead

<!--   FIRST NAME VALIDATION   -->
var strFilter = /^[A-Za-z]*$/;
var fname = $('#fname');
fname.val(fname.val().replace(/^\s+|\s+$/g,''));


if ( ( !strFilter.test(fname.val()) ) || fname == '') {
    alert("Please enter a valid first name\r\n (only characters)");
    $('#fname').css('background-color','#DFE32D');
    $('#fname').focus();
    $('#fname').val('');
    return false;
}
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • The OP did not say he's using jQuery. But nevertheless you could chain the `$("#fname")` calls. – pimvdb Nov 23 '11 at 19:11