Need help with validation (pretty much the opposite to isNaN)

Hi there.

I'm currently doing a pretty rudementary JavaScript assignment for university and would appreciate any help with a problem I am having.

The basic premise of the JavaScript I have been asked to write is an 'insurance calculator' and so the users details can in inserted into variables within the program I have created a basic popup, which has a form for inputting the users data.

With the numerical data I know how to use the 'isNan' to make sure that only numerical data can be entered (therefore validate it to some extent), but part of the assignment requires that I take the users name and I was wondering if there was some JavaScript feature similar to isNaN, which determines whether the information in a text box is actually text or not?


Oh also if anyone cares to help me further... all information is accessed using buttons and ideally I'd like to have the "show table" buttons greyed out until the user actually enters information using the 'disabled' function... ala "<input type="button" value="show results" onClick="showResults();" disabled/>", but is there a feasable way to change this dynamically so that nothing needs refreshing to access the results?

Thanks again :)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Well, everything entered into a text input or textarea is text... ;-)

But I take it that you mean you want to accept only non-numeric characters?

Use a regular expression for that:

var text = document.myForm.textInput.value;
var digits = /\d/;
if( digits.test(text) )
  alert("No numbers, please!");

or if you want only letters, spaces, dashes, commas, periods or apostrophes, you can use

var text = document.myForm.textInput.value;
var notNameChars = /[^a-z \-\.\,']/gi;

if( notNameChars.test(text) )
  alert("Letters, spaces, dashes, commas, periods or apostrophes only please!");

(So you can accept names like "Bill O'Reilly", "John Smith, Jr.", "Lisa-Marie Presley", etc.)

If you want only letters, use

var notLetters = /^[a-zA-Z]/gi;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Everything in a text box is considered text by Javascript, there is no strict data typing. You must ask also how do do define the diffrence between text and numbers. isNaN will tell if is a number so if it retrns false then perhaps it is text, there is one definition.

Can't give you an exact answer since I don't know what you're using to trigger it, but you should be able to use the removeAttribute() method.


var myButton = document.myFormName.myButtonName;
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

basically there is no equivalent to isNaN for text every thing is text, because of no data typing. I suppose isNaN(Variable)= true means  Variableis is text is the best defintion you can make,


is this a homework assignment?  if so, it is a no-no to ask for help

<input type="button"  name="results" value="show results" onClick="showResults();" disabled />

<input type=text onchange="this.form.results.disabled=false" />
> <input type=text onchange="this.form.results.disabled=false" />

I've experienced issues in some browsers when (un)setting binary attributes in this way in pages declared with and actually conforming to an XHTML doctype, which is why I suggested using removeAttribute(), which seems to work correctly in all browsers that support the disabled attribute, no matter what the doctype is or how you access the element.


<input type="button"  name="results" value="show results" onClick="showResults();" disabled />

is neither valid HTML nor valid XHTML.

*Valid HTML: <input type="button"  name="results" value="show results" onClick="showResults();" disabled>

HTML does not allow the <tagname /> construct. Browsers will parse it as just more tag soup, but a validator will not pass this because it breaks the HTML DTD.

*Valid XHTML: <input type="button"  name="results" value="show results" onClick="showResults();" disabled="disabled" />

XHTML does not allow attribute minimisation. See

And I agree, lending a hand is one thing, but doing someone's homework for them outright is doubleplus ungood. Like they say: "Give a man a fire, and he'll be warm today; set a man on fire and he'll stay warm the rest of his life." <grin>
This cool, needs some more work though

<form onmousemove="(isNaN(t1.value)==1)? b1.disabled=true: b1.disabled=false">
<input type="text" value ="datahere" name="t1" >
 <input type="button" name="b1" value="calculate" >         
VincentPuglia, I know what you are saying there is no consistent way of defining text and numbe.r What is 3e4 as you say?, a convention must be adopted
Xxavier: 3e4 --> scientific notation

Zontar: agreed -- only reason I steer away from xxxAttribute() is it's  'new' browser DOM; but then again so is 'disabled'.  -- set the torches; and sweep up the ashes, I really think this sounds to much like an 'assignment'

Vinny, ,  3e4 ,  I am well aware it is sciientific notation.
sorry, still haven't had my morning mugs of espresso
and was thrown by:
>>What is 3e4

"still haven't had my morning mugs of espresso"

I know the feeling. :-)
craigweb2kAuthor Commented:
Vincent: this is a homework assignment (is it still considered homework at 22? :), but the bits I have asked for 'help' with are extra parts I'm adding to strengthen the work (they werent asked for) besides, aslong as I understand how something works within the JS my tutor said its okay!! :)

Xxavier and everyone else who helped with the button problem Many thanks... still dont know if its possible to split them points up, but Zontar did help me with my JavaScript problem and he was 1st, so I hope you guys understand :P
Hi craig,

   Since this isn't one of the boards I moderate on, it didn't really matter to me -- and yes, it's still homework, and yes, I agree with your tutor -- as long as you learn, the experience is good.

  I had raised the issue because I've been told it is the board's policy not to help with homework assignments.  And, it is my experience that most people who ask for homework help usually don't know what they are learning, aren't interested in learning, and just want to do the assignment -- period.

  As long as you understand Zontar's regEx -- fine by me, and good for you. :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.