Menu
How can I check and see if a string has all the numbers(0-9) in it and return false if it does not? I have already tried these two methods:
string.match(pattern)
isNaN(parseInt(string))
I used the pattern /[0-9]/ and also /[0-9]/g. As far as the value in string goes, it could be anything.
Only problem is that both of these methods return the first element ([0]) in the string. If we have a mixed string, for example ‘5yu45’, it would return true (because first element is a number) and give us the false result, although the string does not contain all the numbers. Any suggestion.
[CODE]<html>
<head>
<title>String testing</title>
<script>
function checkWholeForm ()
{
var textString= ''
var idString = form.string.value;
var firstChr = idString.charAt(0);
var numPart = idString.slice(1);
var chrTest = /[uU]/;
var numTest = /[0-9]/g;
if (idString == "")
textString += "Error 1n"
else if (!(firstChr.match(chrTest)))
textString += "Error 2n"
else if (numPart == "")
textString += "Error 3n"
//Either use this or the next
else if (isNaN(parseInt(numPart)))
textString += "Error 4n"
else if (!(numPart.match(numTest)))
textString += "Error 5n"
else if (!((idString.length == 6) || (numPart.length == 5)))
textString += "Error 6n"
if (textString){
alert (textString);
return false;}
return true;
}
</script>
</head>
<body bgcolor="#FBFDFF">
<FORM name='form' action='/.....' method='post' target='_blank' onSubmit='return checkWholeForm(this)'>
<input type="text" name="string" />
<INPUT TYPE='submit' NAME='submit4' VALUE='Submit'>
</body>
</html>[/CODE]
[code=php]
function IsNumber(str) {
var i = 0;
var v = true;
var sarr = str.split('');
for (i=0; i<sarr.length; i++) {
if ((sarr[i] < '0') || (sarr[i] > '9')) { v = false; } // anything outside range?
}
return v; // return true or false (or use 0 and -1 instead)
}
[/code]
How can I check and see if a string has all the numbers(0-9) in it and return false if it does not?[/QUOTE]
[CODE]<SCRIPT type='text/javascript'>
function hasAllDigits(s)
{
for(var i=0; i<10 && s.indexOf(String.fromCharCode(i+48))!=-1; i++)
;
return i==10;
}
alert(hasAllDigits("0a12b34c56g78j9"));
alert(hasAllDigits("93cde210abfhj"));
alert(hasAllDigits("3578960142"));
</SCRIPT>[/CODE]
[CODE]function check10(str){
var n=10, A=[];
while(n>0){
if(!RegExp(--n+'').test(str))A.push(n);
}
if(A.length> 0) alert( 'You are missing '+A.sort());
else // (process valid string)
}[/CODE]
[CODE]function check10(str){
var n=10;
while(n>0){
if(!RegExp(--n+'').test(str)) return false;
}
return true;
}[/CODE]
<i>
</i><script type="text/javascript">
var myStrings=['567y8','54-,.','1234'];
var i=0, s
while(s=myStrings[i++]){
alert(Boolean(!s.match(/D/)))
}
</script>
<i>
</i><script type="text/javascript">
var myStrings=['567y8','54-,.','1234','12.50'];
var i=0, s
while(s=myStrings[i++]){
alert(Boolean(Number(s)))
}
</script>
[CODE]<html>
<head>
<title>String testing</title>
<script>
function checkWholeForm ()
{
var textString= ''
var idString = form.string.value;
var firstChr = idString.charAt(0);
var numPart = idString.slice(1);
var chrTest = /[uU]/;
var numTest = /[0-9]/g;
if (idString == "")
textString += "Error 1n"
else if (!(firstChr.match(chrTest)))
textString += "Error 2n"
else if (numPart == "")
textString += "Error 3n"
//Change to original code
else if (!(IsNumber(numPart)))
textString += "Error 4n"
else if (!((idString.length == 6) || (numPart.length == 5)))
textString += "Error 6n"
if (textString){
alert (textString);
return false;}
return true;
}
function IsNumber(str) {
var i = 0;
var v = true;
var sarr = str.split('');
for (i=0; i<sarr.length; i++) {
if ((sarr[i] < '0') || (sarr[i] > '9')) { v = false; } // anything outside range?
}
return v; // return true or false (or use 0 and -1 instead)
}
</script>
</head>
<body bgcolor="#FBFDFF">
<FORM name='form' action='/.....' method='post' target='_blank' onSubmit='return checkWholeForm(this)'>
<input type="text" name="string" />
<INPUT TYPE='submit' NAME='submit4' VALUE='Submit'>
</body>
</html>[/CODE]
Thank you guys!! I tested all the suggestions and they work fine. My favorite was JMRKER suggestion because it was very easy to implement in my program. I appreciate your help; all suggestions were great.Your error messages could be more informative, however could you explain how a function that detects a non-digit, can be used to detect the presence of all the digits 0-9?
All I do now is call the IsNumber function and pass it my string (NumPart). If IsNumber function returns false, I display a message, if not control is passed to next 'if else' statement.[/QUOTE]
I agree that messages should be more descriptive. I apologize, I was just trying to save some space. I am not sure which function are you referring to? Can you please post the name of the function here?You asked to be able to
Duke[/QUOTE]
[CODE]check and see if a string has all the numbers(0-9) in it and return false if it does not[/CODE]
and it seems that you are using the function [I]IsNumber[/I] in an attempt to accomplish that task. Have I misunderstood, and if so what is your code attempting to validate?[B][snip][/B]I hope it makes sense. The code is a complete example. Please feel free to test and play with it and letI told you what [I]isNumber[/I] does, having discerned it in less time than it would have taken to read your explanation.
me know if you find anything otherwise.
Duke[/QUOTE]
[CODE]function checkWholeForm(str)
{
return /^ud{5}$/i.test(str.replace(/^s+|s+$/g,''));
}[/CODE]
How can I check and see if a string has all the numbers(0-9) in it and return false if it does not?[/QUOTE]
[CODE]
String.prototype.isDigits = function() { return /^d+$/.test(this.toString()); }[/CODE]
[CODE]
String.prototype.isNumeric = function() { return /^[+-]?d+.?d*$/.test(this.toString()); }[/CODE]
[CODE]
if(document.formName.fieldName.value.isDigits())
{
alert("isDigits returned a true result.");
}
else
{
alert("isDigits returned a false result.");
}[/CODE]
Your code appears to be testing for 'u' followed by five digits, in which case provided you don't need detailed error messages, it boils down to:
[CODE]function checkWholeForm(str)
[/QUOTE]
{
return /^ud{5}$/i.test(str.replace(/^s+|s+$/g,''));
}[/CODE]
0.1.9 — BETA 5.18