Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number and space validation

Status
Not open for further replies.

BHaines

Programmer
May 29, 2003
100
US
I have an ASP page with a form in it. In the form there is a text input box for an account number. Users can enter multiple account numbers in it and often do. The department using the app wants it to check that only numbers and spaces are in that box. I've got code to check for numbers only, and that works great, but how in the Sam Hill can I get it modified to check for spaces as well. I know Trim will take spaces out of the left or right sides of a value, is there something that will strip them out of a whole string before I pass the value to the vbscript code below for IsNumeric validation?

function AccountCheck
Dim StartAccount
set StartAccount = window.event.srcElement
if StartAccount.value <> &quot;&quot; then
if Not IsNumeric(Trim(StartAccount.value)) then
StartAccount.Style.Color = &quot;#FF0000&quot;
MsgBox &quot;This value must be a numbers only.&quot;, 64, &quot;Invalid Account Number&quot;
StartAccount.Focus
else
StartAccount.Style.Color = &quot;#000000&quot;
end if
else
StartAccount.Style.Color = &quot;#000000&quot;
end if
end function
 
replace function
Replace(expression, find, replacewith[, start[, count[, compare]]])
or
regex (regular expression) replace method
pattern would be \s which matches any white space. This will match tab's etc.. also.


____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
If I understand correctly you have a box where the users will enter account numbers separated by spaces and you're trying to validate that each of the things between spaces is a number.

I would suggest this, using strAccounts as the value (this is VBScript -- your post looks like VB):
Code:
'Strip off any ending and beginning spaces
strAccounts = Trim(strAccounts)

'Remove any double spaces
While InStr(strAccounts, &quot;  &quot;) <> 0
  Replace(strAccounts, &quot;  &quot;, &quot; &quot;)
End

'Divide the account numbers apart
Dim aryAccountItems
aryAccountItems = Split(strAccounts, &quot; &quot;)

'Loop through them and verify that they're all numbers
Dim i
For i = 0 to UBound(aryAccountItems) - 1
  If Not IsNumeric(aryAccountItems(i)) Then
    'Do your error thing, set a flag, etc.
  End If
Next
Then if you want to put it back together again (assuming everything was OK) you'd just use
Code:
 strAccounts = Join(aryAccountItems, &quot; &quot;)
.
 
wow, that's a whole lot of validation for spaces. [wink]

regex would be the way to go really if you want to get into that deep of a process.
eg(sense we're posting code)[wink]:
<%
Dim frmVal
frmVal = &quot;1 1 1 1235 5&quot;

Function ReplaceSpaces(val)
Dim regEx, patrn
Set regEx = New RegExp
regEx.Global = True
regEx.Pattern = &quot;[\s]&quot;
ReplaceSpaces = regEx.Replace(val, &quot;&quot;)
End Function

frmVal = ReplaceSpaces(frmVal)

If cLng(frmVal) Then
response.write frmVal
else
respone.write &quot;conversion error!&quot;
end if
%>

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
O' to validate the numeric the pattern adds \d

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
clarify the last post on \d validation pattern for numeric values. You need to perform a test on that other then the replace method. Although the Clng use does this in the conditional statement up there. just in case that was as it sounded, misleading.

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
Agreed that a regexp would be more expedient. I was just hoping to expose the OP to more string modification techniques. The splitting will still be necessary for working with the individual numbers, of course.
 
[thumbsup2] on the string manip comment! I like to see the educational factor in our posts. Leads to better and more productive threads.

the regex could split the numric values out fine also and validate without the split. I'm not sure but the FAQ I added on regex string amnipulation may perfomr something similar hmmm.....

nope, checked and I split on &quot; &quot;. have to rewrite that one now that I have the idea. [smile]
faq333-3645 in case your intersted

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
I looked at the faq -- well done! -- but I'm not sure how you can use regexp to divide it up if you don't know how many account numbers to expect. I'd love to learn more, so please post it here or in the faq!

(I used to use regexps all the time when I was writing a lot of Perl in the early days of the web, but I know I've forgotten a fair bit.)
 
The Match() method would allow you to return a collection of matches that met your pattern, so basically you would be able to specify a greedy pattern to get the matches out. This will also help in situations where there are multiple spaces between numbers, tabs, etc. that would cause you to have ascrewy array if you used split.
So basically I would suggest one pattern for testing and one pattern for getting the matches.

I would validate using &quot;[\s\d]+&quot;
and match using &quot;[\d]+&quot;

Only the greediest matches should be returned on the match pattern, so you should have a nice little collection of all of the numbers:
Code:
Dim matches, match

Set matches = Regex.Match(AcctNumStr)
For Each match in matches
   Response.Write &quot;Acct: &quot; & match & &quot;<br>&quot;
Next

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
[smile]

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
he did that cause I told him how [rofl2]

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
Heh, yep, thats it...

riiiiiiiiiiiiiiiiiiiiiiiiiiiight....
;)

notsobrightidea.gif
tricycle.gif
wedgie.gif
makefun.gif
 
well, you know i, i, i could have iiiiif iii reallyyyy wanted to
[swords]

____________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
 
Ok, I've attempted to make Genimuse and onpnt's code work with mine, but all I get are errors. This has to trigger with onBlur, not when the form is submitted. When the user types something into the input box, then they remove focus from that box it has to run the check on the value currently in there and send back the alert box and refocus in that box if there are any letters or non-number or non-space characters in the box.

The code I have now (posted at the top) successfully checks if there is a single number in there, but it needs to determine if there are only numbers and spaces. I have no reg exps experience, and all the reg exps posts were entirely over my head. As was the FAQ.

Of note, Account numbers are not a set length, they vary widely, but only contain numbers from 0-9. A user might enter 2, 3, 4, 5 however many accounts they want, seperated by spaces. Is there a way I can evaluate this when OnBlur triggers the function?
 
if you need a onBlur event then you need to do this with client scripting and not ASP. you can convert the regex easiely to javascript or vbscript but if that is a problem then I would go to the javascript forum216 or vbscript forum329 and post this question

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
Thanks, I've posted to the Javascript and VBScript forums.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top