Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to find out if a string contains number

Posted on 1997-07-31
3
Medium Priority
?
1,104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
pmcintos earned 450 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

715 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