Solved

How to find out if a string contains number

Posted on 1997-07-31
3
1,078 Views
Last Modified: 2012-05-04
Its really the oposite problem i have.
I have a string and want to find out if there are some character in it, or  blank space. Its a typically prize and i want it on the form 200000 NOT 200 000 or 200.000 f.ex
 
0
Comment
Question by:carlarne
3 Comments
 
LVL 1

Accepted Solution

by:
pmcintos earned 150 total points
ID: 1268791
I have a little trouble understanding the question...
I beleive that you need a way to examine a string and see if it contains a certain number or not...
This is the way I would solve this problem:

var checkvar = "200000";
//get this next variable from the form.
var str = document.Form.str.value;

for ( var i = 0; i < str.length; i ++ ) {
  if ( str.substring( i, (i + checkvar.length) ) {
   alert ( "Found " + checkvar + " in the string searched!");
  }
}

If you are looking for particluar number that is the way you would do it.  If you need to inspect for any number then you have to use another for loop inside the first one and just compare each character of the checkvar ( say checkvar = "0123456789" ) with each character of the string you want to check.

If this isn't the question you asked then e-mail me a revised version and I'll try to help with that.

Pete


0
 
LVL 3

Expert Comment

by:garik
ID: 1268792
I'm too late, but since I typed it anyway:

<HTML>
<HEAD><TITLE>Test</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function check(str) {
for(err="", i=0; i<str.length; i++, err+="_") {
  ch = str.charAt(i);
  if(ch < '0' || ch > '9') {
    alert("Bad number:\n"+err+"v\n"+str+"\n");
    return;
  }
}
alert("Good number:\n"+str);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=f>
<INPUT NAME=num SIZE=10>
<INPUT TYPE=BUTTON VALUE=Check onClick='check(f.num.value)'>
</FORM>
</BODY>
</HTML>

0
 

Author Comment

by:carlarne
ID: 1268793
Thanks, its working very good
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now