0

I have already got this working, but I know there must be a more succinct way of doing it.

Using onBlur on a text field in the html page, it calls a method which replaces the value of the string if the value is empty or NaN.

JS:

function checkStr(str) {
        return str.value = "0";
}

HTML:

onblur="if(this.value=='' || isNaN(this.value)){this.value=checkStr(this.value)};"

So this does work, but seems exhaustive. Using:

function checkStr(str) {
    if (str.value == "" || isNaN(str.value)) {
        return str.value = "0";
    }
}

In the JS file, and:

onblur="checkStr(this.value);" 

In the HTML doesn't work.

Any ideas?

4
  • You said you tried onblur="checkStr();" in the HTML... did you pass the value? Like this: onblur="checkStr(this.value);" Commented Jul 29, 2012 at 17:34
  • Yes I did, sorry that was an error. Corrected now. Commented Jul 29, 2012 at 17:40
  • What do you want to do with the return value? Are you trying to set the input field to 0 if they don't enter a number? Commented Jul 29, 2012 at 17:42
  • Yes, exactly that. On exiting the field if the value is null or NaN it should set the value to 0. Commented Jul 29, 2012 at 17:43

4 Answers 4

1

Your second method isn't assigning anything to textbox value, so you really need the following , i.e. passing in the textbox into the function

onblur="checkStr(this);"

and then for the function to check and assign the textbox

function checkStr(textbox) {
    if (textbox.value == "" || isNaN(textbox.value)) {
        textbox.value = "0";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

str is your string, so you don't need to do str.value:

function checkStr(str) {
    if (str == "" || isNaN(str)) {
        return "0";
    }
}

And set onblur to actually set the input's value (as @chris mentions.)

Comments

0

What you tried looks close, but if you pass this.value to the function, then you shouldn't check str.value within the function.

Also, you need to return the correct value if the validation passes. And assign the value back to the field when the value returns.

I think you need something like this:

<input type="text" onblur="this.value = checkStr(this.value);" />​

Then in the js...

function checkStr(str) {
    if (str == "" || isNaN(str)) {
        return "0";
    } else {
        return str;
    }
}

1 Comment

provide both by all means as it proves an working answer - it just annoys me to just see the fiddle
0

If I'm correct in my assumptions, and you're trying to set the value to 0 if the current value is not a number, or there is no value, I'd suggest (if you must use in-line JavaScript):

onblur="checkStr(this);"

Coupled with:

function checkStr(el) {
    el.value = !!!(1 * el.value) ? 0 : el.value;
}

JS Fiddle demo.

Comments

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.