Solved

How to find out if a string contains number

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

830 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