Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to find out if a string contains number

Posted on 1997-07-31
3
Medium Priority
?
1,105 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

972 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