Solved

How to find out if a string contains number

Posted on 1997-07-31
3
1,086 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

895 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

14 Experts available now in Live!

Get 1:1 Help Now