two questions..

1. Senior Member
Join Date
Dec 1969
Posts
935

## two questions..

Hi all.<BR>(1) I have a field in my form (let&#039;s say address) and I have a variable (let&#039;s say hlp).<BR>I want to add 1 to hlp if the length of the address is more than 0.<BR>I want to do it with out if statement.. so I thought of doing something like this:<BR>hlp = hlp + Cint(1/Len(Trim(Request.Form("address"))))<BR>the problem is that I will always get 0.. I want the upper integer value of the division. how can I do that?<BR><BR>(2) I want to check the form that was submitted by the user with my asp script befor I insert the data to my database.<BR>if there&#039;s a problem with the form I want to display the form with all of the data that the user gave in the right fields and write him what was wrong with the form.<BR>What I came with is to have a variable (let&#039;s say sum) which I set to 1 at the start and in each check I do for the form (if the check comes up with a problem) I multiply sum with a prime number (each time a different one).<BR>after I checked everything I check if sum is 1 if so all is good, otherwise I display the form and for each field I check if sum is divided by the prime number.<BR><BR>is there a better way to do what I want???<BR>I mean sum can be a very large number if there are a lot of things to check and a lot of them have problems...<BR><BR>thanks!

2. Senior Member
Join Date
Dec 1969
Posts
5,955

## I like your first question...

...even though I don&#039;t understand why you don&#039;t want to use an If statement. I used to call your way of solving this "mechanical programming", because there aren&#039;t any If&#039;s involved - it&#039;s just going through the formula, like cogs in a machine. :-)<BR><BR>Anyway, to answer (1): hlp = hlp + (1 - CInt(1/(Len(...) + 1)))<BR><BR>Your problem was with Len() = 0, whereby you&#039;d do a divide by zero, which should have thrown up an error. Example:<BR><BR>Len(): 0<BR>Len() + 1: 1<BR>1/(Len()+1): 1<BR>CInt(1/(Len()+1)): 1<BR>1-CInt(1/(Len()+1)): 0<BR><BR>Good so far:<BR><BR>Len(): 1<BR>Len()+1: 2<BR>1/(Len()+1): 0.5<BR>CInt(1/(Len()+1)): 0 (I hope...)<BR>1-CInt(1/(Len()+1)): 1<BR><BR>Anyway, it&#039;s rather complicated, but I think it should work.<BR><BR>So, why don&#039;t you want to use an If then?<BR><BR>Oliver.

3. Senior Member
Join Date
Dec 1969
Posts
5,955

...question: What you do probably works, but why use prime numbers? Why not do it binary? Start with a value of zero (0). So, if the whole thing runs and the variable is still zero, you know everything is fine. As you check each field and it turns out there&#039;s a problem, just add 1, 2, 4, 8, 16, etc., which are the decimal representations of the binary value 0b00001, 0b00010, 0b00100, 0b01000, 0b10000. Adding them together is like setting each bit, so if fields 1, 2 and 3 are wrong, you add 1, 2 and 4 = 7 which is 0b00111.<BR><BR>On the page where you display the errors, all you need to do is check the value&#039;s bits, like this:<BR><BR>If ErrorCode And 1 Then &#039; field 1 is faulty, so display error<BR>...<BR>Else If ErrorCode And 2 Then &#039; field 2 is faulty...<BR>...<BR>Else If ErrorCode And 4 Then &#039; field 3 is faulty...<BR>...<BR>Else If ErrorCode And 8 Then &#039; field 4 is faulty...<BR>...<BR><BR>See what I mean. I haven&#039;t done this in VBScript myself yet, so there may be a couple of bugs in the code above, but I hope you understand the principle.<BR><BR>Oliver.

4. Senior Member
Join Date
Dec 1969
Posts
935

## ****!

well, first of all, you&#039;re right! I do have a problem with the zero length situation...<BR>I already so that problem so I fixed it but I gave the wrong idea... the new one is this len(...)/251<BR>while 251 is the maxlength of the field.. so I&#039;ll have a number which is in this range: [0,1)....<BR>I don&#039;t see how this: hlp = hlp + (1 - CInt((Len(...)/251 + 1))) will solve it.. if the length is 0 I will have hlp = hlp + 2 and that&#039;s not good for me.. I need hlp = hlp + 1 if the length is more than 0 and hlp = hlp + 0 otherwise..<BR><BR>and for why I want it this way:<BR>it&#039;s not that I have anything against if statements, it&#039;s just that I need to do this for more than one form fields and I think it just messes the code... too much "if"s for me.

5. Senior Member
Join Date
Dec 1969
Posts
5,955

## No, you're not using...

...what I posted - you&#039;re mixing things up. Try out the formula that I posted and see whether that works. It should work for any length string (I hope - didn&#039;t test it).<BR><BR>Your idea of simply dividing by the maximum length of the string won&#039;t work either, because CInt(251/251) is 1, which is good, but CInt(250/251) is 0 (I think - if not, then my formula won&#039;t work). However, CInt(0/251) is 0 also, which is good.<BR><BR>I assume that CInt() rounds down to the next integer. If it doesn&#039;t then you need to use Int() or Fix().<BR><BR>Oliver.

6. Senior Member
Join Date
Dec 1969
Posts
935

## okay, this is nice...

it&#039;s a good idea, but let&#039;s see if I get you right:<BR>let&#039;s say there were problems in fields (1,3,4,5) so the number I will have is: 1 + 4 + 8 + 16 = 29, and it should look like this in binary form: 11101<BR><BR>now that I have that what do I do?? move on the binary form as a string and check for &#039;1&#039;s???

7. Senior Member
Join Date
Dec 1969
Posts
5,955

## Do exactly what you were...

...going to do with your prime number generated value. The binary value you&#039;re getting is just an integer, in your example 29. You can pass this on just like you would have done with your prime number value.<BR><BR>Oliver.

8. Senior Member
Join Date
Dec 1969
Posts
5,955

## Also, why don't you give up and...

...use an If statement? What&#039;s the big deal with the If?<BR><BR>Oliver.

9. Senior Member
Join Date
Dec 1969
Posts
935

## oh, okay!

I didn&#039;t notice that the +1 in your formula is inside the Cint..<BR>your formula does work, thanks!<BR><BR>and for your question, when you do Cint(x) and 0 &#060;= x &#060;= 0.5 you&#039;ll get 0, if 0.5 &#060; x &#060; 1 you&#039;ll get 1.

10. Senior Member
Join Date
Dec 1969
Posts
5,955

## I think you'll need to...

...replace CInt() with Fix(). I think Fix(0.5) is 0, as well as Fix(0.6) and Fix(0.4). Then the forumla should be fine.<BR><BR>Oliver.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•