Solved

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

Posted on 2003-10-28
14
983 Views
Last Modified: 2008-02-01
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?

Thanks.

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 :)
0
Comment
Question by:craigweb2k
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 11

Accepted Solution

by:
Zontar earned 300 total points
ID: 9638905
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!");
  document.myForm.textInput.focus();
}

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!");
  document.myForm.textInput.focus();
}

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

etc.
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 9638942
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.

Xx
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9638982
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.

e.g.

var myButton = document.myFormName.myButtonName;
myButton.removeAttribute("disabled");
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 9639031
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,
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9639074
Xavier:
alert(isNaN(3e5))
alert(isNaN(1))
alert(isNaN('a'))

craig:

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" />
0
 
LVL 11

Expert Comment

by:Zontar
ID: 9639323
> <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.

BTW,

<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 http://www.w3.org/TR/xhtml1/#h-4.5

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>
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 9639408
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" >        
</form>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:Xxavier
ID: 9639441
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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9641483
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
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 9642207
Vinny, ,  3e4 ,  I am well aware it is sciientific notation.
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9642462
sorry, still haven't had my morning mugs of espresso
and was thrown by:
>>What is 3e4

Vinny
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 9642994
"still haven't had my morning mugs of espresso"

I know the feeling. :-)
0
 

Author Comment

by:craigweb2k
ID: 9644685
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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9644736
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. :)

vinny
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now