0

I'm trying to validate a form field and am new to javascript. I have been using !isNaN but this only seems to detect if there is absolutely no numbers. For example, in the below example if I have 'Willy, Fred5' entered into the input field it seems to detect the strings and does not return false. What I need to do is detect if there is even one numeric value in the input field. But can't seem to find an answer that works.

function fnCheckName(strName) {
    strName.style.background = "#FFFFFF";
    var nameChk = strName.value;
    if (nameChk.indexOf(', ') < 0 || nameChk == "" || !isNaN(nameChk){
        strName.style.background = "#FBEC5D";
        return false}
        else {return true}
Danrex
  • 1,657
  • 4
  • 31
  • 44
  • 1
    http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric – falcon May 21 '14 at 04:06
  • try regexp class in JavaScript and create a regular expression for it. – Mozak May 21 '14 at 04:07
  • Nice comedy, falcon... that links to a question marked as a duplicate! –  May 21 '14 at 04:07
  • 1
    @Jeremy, corrected before you responded – falcon May 21 '14 at 04:07
  • Still doesn't seem to work. – Danrex May 21 '14 at 04:24
  • Appart from the *if* condition having a syntax error, `!isNaN(nameChk)` will return true only if [*isNaN*](http://ecma-international.org/ecma-262/5.1/#sec-15.1.2.4) returns false, i.e. *nameChk* **is** a number (or can be coerced to a number). – RobG May 21 '14 at 04:47
  • @RobG `!isNaN("foo1bar")` returns false, but the OP expressed interest to "detect if there is even one numeric value in the input field". (Some languages/libraries would have accepted such a conversion to a number as 1.) – user2864740 May 21 '14 at 05:08

5 Answers5

2

You need to add RegExp:

function fnCheckName(strName) {
strName.style.background = "#FFFFFF";
var nameChk = strName.value;
if (/\d/.test(nameChk)){
        strName.style.background = "#FBEC5D";
        return false
    }
    else {return true}

/^[a-zA-Z]+$/ - checks for only letters (true for "fdsfjklr")

/^([^0-9]*)$/ - checks for only numbers (true for "4321")

/\d/ - checks for any string with number (true for "fdslkj45")

deadulya
  • 686
  • 6
  • 15
2
/\d/.test('foo5')
outputs: true

/\d/.test('foo')
outputs: false

/\d/.test('5')
outputs: true

So given those inputs and outputs, I think your modified code would be:

function fnCheckName(strName) {
  strName.style.background = "#FFFFFF";
  var nameChk = strName.value;
  if (nameChk.indexOf(', ') < 0 || nameChk == "" || !/\d/.test(nameChk)) {
    strName.style.background = "#FBEC5D";
    return false
  }
  else {
    return true
  }
}
000
  • 26,951
  • 10
  • 71
  • 101
  • I think the OP want's the inverse (really non-negated) test form for "contains a digit".. but using `/\d/` (or equivalent) is suitable for the task. – user2864740 May 21 '14 at 05:04
  • I couldn't really tell, the question is pretty unclear. If they want the inverse then they can remove the !. – 000 May 21 '14 at 05:09
  • Thanks for that. yes needed to take out the ! but it worked thank you so much. – Danrex May 21 '14 at 08:03
0

Try this Danrex

function fnCheckName(strName) {
    strName.style.background = "#FFFFFF";
    var nameChk = strName.value;
    var regNew = new RegExp(/^([^0-9]*)$/);
    if (regNew.test(nameChk)) {//If true i.e. no digit is in the string

    } else {//If false i.e. digit is in the string  

    }
}

or

function fnCheckName(strName) {
        strName.style.background = "#FFFFFF";
        var nameChk = strName.value;
        var regNew = new RegExp(/^([^0-9]*)$/);
        return (regNew.test(nameChk));
    };
Mozak
  • 2,738
  • 4
  • 30
  • 49
-1

Try this changes,

function fnCheckName(strName) {
strName.style.background = "#FFFFFF";
var nameChk = strName.value;
var status = false;
for(var i=0;i<nameChk.length;i++){
  if(isNan(nameChk[i])==false)
    status = true;
  else
    status = false;
}
return status;
}
Surendheran
  • 187
  • 1
  • 18
  • I keep getting an error - uncaught type error - undefined is not a function on the if() line. – Danrex May 21 '14 at 04:33
  • [`isNaN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) is a global function, not a Number method .. also a break/return is missing, so even with the isNaN change this is only "true" if the *last* character is not-NaN. – user2864740 May 21 '14 at 04:58
  • It's still wrong because of a missing break/return - status will be reset because the loop is not terminated correctly. Try with the values "1foo" and "bar2" as both of these contain numbers, but this function will only return true in one case. (This is also a very curious use of `isNaN`, although I believe it holds that `!isNaN(x) === /\d/.test(x)` for all single-character strings x ..) – user2864740 May 21 '14 at 06:09
-1

If you enter even one number return false else return true

eval('/^[a-zA-Z]+$/').test('sfjd');    

EDIT

function fnCheckName(strName) {
        if( eval('/^[a-zA-Z]+$/').test(strName))
        {
            // this name not contains numbers
            // change background or every things that you want
            return true;
        }
        else
        {
            // this name has number or numbers
            // change background or every things that you want
            return false;

        }
    }
Hamid Bahmanabady
  • 665
  • 1
  • 8
  • 20
  • I don't even know how to put this in or what it is? – Danrex May 21 '14 at 04:34
  • The use of `eval` is *"just wrong"* (it will actually have equivalent semantics if removed so `eval` is *entirely unnecessary and useless* here). That aside, this will fail and return "true" for input like `=^_^=`. – user2864740 May 21 '14 at 05:01