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}
7
  • 1
    stackoverflow.com/questions/18082/… Commented May 21, 2014 at 4:06
  • try regexp class in JavaScript and create a regular expression for it. Commented May 21, 2014 at 4:07
  • Nice comedy, falcon... that links to a question marked as a duplicate! Commented May 21, 2014 at 4:07
  • 1
    @Jeremy, corrected before you responded Commented May 21, 2014 at 4:07
  • Still doesn't seem to work. Commented May 21, 2014 at 4:24

5 Answers 5

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
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
I couldn't really tell, the question is pretty unclear. If they want the inverse then they can remove the !.
Thanks for that. yes needed to take out the ! but it worked thank you so much.
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")

Comments

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));
    };

Comments

-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;

        }
    }

2 Comments

I don't even know how to put this in or what it is?
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 =^_^=.
-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;
}

3 Comments

I keep getting an error - uncaught type error - undefined is not a function on the if() line.
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.
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 ..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.