Wrox Programmer Forums
|
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 25th, 2004, 06:37 AM
Authorized User
 
Join Date: Jul 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation Question

Just starting to really get into hand coded form validation.

I have this bit of code that does a fairly good job. I just want to make the phone number validation so that people can use a hyphen when they type a phone number. Can anyone show me how or help me with it.I would also like to control the date range for the dates. Say it would only take a date before or after a specific date.


Code:
<script language=vbscript>
Sub btnSubmit_OnClick()
    If isdate(honotice.txtlossdate.value) = 0 Then
        Alert "You must enter a loss date. If you do not know the loss date please call 1-800-000-0000"
        honotice.txtlossdate.focus
        Exit Sub
    ElseIf  Len(honotice.txtinsuredname.value) <2 or IsNumeric(honotice.txtinsuredname.value)then
        Alert "You must enter the insured. If you do not know the insured name please call 1-800-000-0000"
        honotice.txtinsuredname.focus
        Exit Sub
    ElseIf Len(honotice.txtpolicynumber.value) = 0 Then
        Alert "You must enter the policy number. If you do not know the policy number please call 1-800-000-0000"
        honotice.txtpolicynumber.focus
        Exit Sub    
    ElseIf isdate(honotice.txtpolicyeffectdate.value) = 0 Then
        Alert "You must enter a policy effective date. If you do not know the policy effective date please call 1-800-000-0000"
        honotice.txtpolicyeffectdate.focus
        Exit Sub
    ElseIf Len(honotice.txthomephone.value) <2 or Not IsNumeric(honotice.txthomephone.value) then
        Alert "You must enter a phone number. If you do not have a phone please call 1-800-000-0000"
        honotice.txthomephone.focus
        Exit Sub            
    End If
    Call honotice.submit()
End Sub
</script>



Thanks!


Reply With Quote
  #2 (permalink)  
Old July 25th, 2004, 08:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You can use Regular expressions

dim objRegx
set objRegx = Ne RegExp
objRegx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"

if (objRegx.test(honotice.txthomephone.value)) then
    'Phone OK
else
    'Phone not OK
end if

Something like that. Regular expressions are pretty powerful, and are definitely very helpful.

Brian
Reply With Quote
  #3 (permalink)  
Old July 25th, 2004, 10:23 AM
Authorized User
 
Join Date: Jul 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks allot!!

I'm going to open my wrox "VBScript reference" book and read about "regular expressions". Well I read chapter seven, four times and starting to understand the logic and objects.It is the implimentation I'm having trouble with. I googled regular expresions and test but could not come up with a good example (HIT ME WITH SHOVEL).

I tried to use your suggestion but I am obviously not doing it correctly. I do apprciate your help. Once I get this down I'm good to go.
Code:
<% 
dim objregx
set objregx
objregx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"

%>


<script language=vbscript>
Sub btnSubmit_OnClick()
    If isdate(honotice.txtlossdate.value) = 0 Then
        Alert "You must enter a loss date. If you do not know the loss date please call 1-800-000-0000"
        honotice.txtlossdate.focus
        Exit Sub
    ElseIf  Len(honotice.txtinsuredname.value) <2 or IsNumeric(honotice.txtinsuredname.value)then
        Alert "You must enter the insured. If you do not know the insured name please call 1-800-000-0000"
        honotice.txtinsuredname.focus
        Exit Sub
    ElseIf Len(honotice.txtpolicynumber.value) = 0 Then
        Alert "You must enter the policy number. If you do not know the policy number please call 1-800-000-0000"
        honotice.txtpolicynumber.focus
        Exit Sub    
    ElseIf isdate(honotice.txtpolicyeffectdate.value) = 0 Then
        Alert "You must enter a policy effective date. If you do not know the policy effective date please call 1-800-000-0000"
        honotice.txtpolicyeffectdate.focus
        Exit Sub
    if (objRegx.test(honotice.txthomephone.value)) then
        Alert "You must enter a phone number. If you do not have a phone please call 1-800-000-0000"
        honotice.txthomephone.focus
        Exit Sub            
    End If
    Call honotice.submit()
End Sub
</script>

Thanks!
Rich
Reply With Quote
  #4 (permalink)  
Old July 26th, 2004, 07:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You need to change this:

set objregx
objregx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"

to this:

set objregx = New RegExp
objregx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"

I don't know why you do it in this manner... everything else is done through CreateObject(), but in all of the examples that I've seen, this is how it is done. Let me know if that works out for you. It may be Regex instead; I can't remember because when .NET came out, they renamed the library.

Brian
Reply With Quote
  #5 (permalink)  
Old July 26th, 2004, 08:30 AM
Authorized User
 
Join Date: Jul 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is tough nut to crack. I appreciate you hanging in there with me. You get to the field on the form and hit submit and it does nothing. I included it all this time.
Code:
<% 
option explicit
Dim objregx
set objregx = New RegExp
objregx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Untitled</title>
    <style>
    form {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 20%;
    border: #6495ED; 
    border: outset;

}

    </style>
</head>

<body>

<form name="honotice" action="add_ho_claim.asp" method="post">
<TABLE border="0" width="90%">
<tr>
    <td colspan="3">
        <div align="center"><strong>
        Homeowners Loss Notice
        </strong></div>
        <strong><p>Required fields are in bold fonts. If you do not have the information
to complete this form you may call the Receiver at 1-800-882-3054 or use our <a href="">contact us</a> form</p></strong>
    </td>
</tr>
<tr>
    <TD>
        Company Name:<input type="text" name="txtcompany" size="30" value="Aries Insurance Company">
    </TD>
    <TD>
        <strong>Loss Date:</strong><input type="text" name="txtlossdate" size="8">
    </TD>
    <td>
        Date Reported:<input type="text" name="txtdatereported" value="<%=date%>" size="8">
    </td>
</tr>
</table>
<TABLE border="0" width="90%">
<tr>
    <td>
        <strong>Insured Name:&nbsp;</strong><input type="text" name="txtinsuredname" size="30">
    </td>
    <td>
        <strong>Policy Number:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="txtpolicynumber" size="15">
    </td>
</tr>
<tr>
    <td>
        Address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="txtaddress" size="30">
    </td>
    <td>
        <strong>Policy Effective Date:</strong>&nbsp;<input type="text" name="txtpolicyeffectdate" size="8">
    </td>
</tr>
<tr>

    <td>
        City: <input type="text" name="txtcity" size="10">
        State: <input type="text" name="txtstate" size="3">
        Zip:<input type="text" name="txtzip" size="10">
    </td>
    <td>
        Company/Agent Selling:<input type="text" name="txtcompanyagentseller" size="20">
    </td>
</tr>
<tr>
    <td>
         <strong>Primary Phone:</strong><input type="text" name="txthomephone" size="12">
    </td>
    <td>
        Agent Address:<input type="text" name="txtagentaddress" size="30">
    </td>
</tr>
<tr>
    <td>
        Alternate Phone:&nbsp;<input type="text" name="txtworkphone" size="12">
    </td>
    <td>
        City: <input type="text" name="txtagentcity" size="10">
        State: <input type="text" name="txtagentstate" size="3">
        Zip:<input type="text" name="txtagentzip" size="10">
    </td>
</tr>
<tr>
    <td>
        Police Report number:<input type="text" name="txtpolicereport" size="11"><br>
        Fire Report number:&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="txtfirereport" size="11">
    </td>
    <td colspan="2">
        Agent Phone:<input type="text" name="txtagentphone" size="12">
    </td>
</tr>
</table>
<TABLE border="0" width="90%">
<tr>
    <td>
        How did loss occur: <em>multiple selections allowed</em><br>
<select name="txtlossdetails" id="txtlossdetails" multiple>
            <option value="Fire or Lightning">Fire or Lightning</option>
            <option value="Windstrom or Hail">Windstrom or Hail</option>
            <option value="Explosion">Explosion</option>
            <option value="Riot of Civil Commotion">Riot of Civil Commotion</option>
            <option value="Aircraft">Aircraft</option>
            <option value="Vehicle">Vehicle</option>
            <option value="Smoke">Smoke</option>
            <option value="Vandalism or Malicious Mischief">Vandalism or Malicious Mischief</option>
            <option value="Theft">Theft</option>
            <option value="Falling Objects">Falling Objects</option>
            <option value="Weight of ice, snow or sleet">Weight of ice, snow or sleet</option>
            <option value="Accidental discharge or overflow of water or stream">Accidental discharge or overflow of water or stream</option>
            <option value="Sudden/accidental tearing apart, craking burning or bulging">Sudden/accidental tearing apart, craking burning or bulging</option>
            <option value="Freezing">Freezing</option>
            <option value="Sudden/accidental damage from artificial generated electric">Sudden/accidental damage from artificial generated electric</option>
            <option value="Other">Other</option>
</select>
    </td>

    <td>
        Describe Loss:<br><textarea cols="35" rows="4" name="txtdescribeloss"></textarea>
    </td>
</tr>
</table>
<table border="0" width="90%">
<tr>
    <td>
        Check box if Injuries are Involved? Yes:
        <input type=checkbox name=injuries value=1>

    </td>
    <td>
        If yes: Injured Party:<input type="text" name="injname" size="30">
    </td>
</tr>
<tr>
    <td>
        Your relationship to injured party:<br>
        <em>spouse,sibling, etc..</em><input type="text" name="txtinjrelation" size="8">
    </td>    
    <td>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        Address:<input type="text" name="txtinjaddress" size="30">
    </td>
</tr>
<tr>
    <td>
        &nbsp;
    </td>    
    <td>
        City: <input type="text" name="txtinjcity" size="10">
        State: <input type="text" name="txtinjstate" size="3">
        Zip:<input type="text" name="txtinjzip" size="10">
    </td>
</tr>
<tr>
    <td>
        &nbsp;
    </td>    
    <td>
        Home Phone:<input type="text" name="txtinjphone" size="12">
    </td>
</tr>
<tr>
    <td align="center" colspan="2">
        <input type="button" name="btnsubmit" value="Submit Notice">
        &nbsp;&nbsp;
        <input type="reset" value="Clear Form">
    </td>
</tr>

</TABLE>
</form>
<script language=vbscript>
Sub btnSubmit_OnClick()
    If isdate(honotice.txtlossdate.value) = 0 Then
        Alert "You must enter a loss date. If you do not know the loss date please call 1-800-882-3054"
        honotice.txtlossdate.focus
        Exit Sub
    ElseIf  Len(honotice.txtinsuredname.value) <2 or IsNumeric(honotice.txtinsuredname.value)then
        Alert "You must enter the insured. If you do not know the insured name please call 1-800-882-3054"
        honotice.txtinsuredname.focus
        Exit Sub
    ElseIf Len(honotice.txtpolicynumber.value) = 0 Then
        Alert "You must enter the policy number. If you do not know the policy number please call 1-800-882-3054"
        honotice.txtpolicynumber.focus
        Exit Sub    
    ElseIf isdate(honotice.txtpolicyeffectdate.value) = 0 Then
        Alert "You must enter a policy effective date. If you do not know the policy effective date please call 1-800-882-3054"
        honotice.txtpolicyeffectdate.focus
        Exit Sub
    ElseIf (objRegx.test(honotice.txthomephone.value)) then
        Alert "You must enter a phone number. If you do not have a phone please call 1-800-000-0000"
        honotice.txthomephone.focus
        Exit Sub            
    End If
    Call honotice.submit()
End Sub
</script>

</body>
</html>

Thanks
Rich
Reply With Quote
  #6 (permalink)  
Old July 26th, 2004, 09:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Didn't notice something earlier when I looked at your code. You need to create the RegExp object at the client side in the VBScript code (exactly as I had before), not at the ASP level. VBScript runs at the client side and can't see anything in the <% %>.

Brian
Reply With Quote
  #7 (permalink)  
Old July 26th, 2004, 12:58 PM
Authorized User
 
Join Date: Jul 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Once again thanks! I broke it down to a one field form and got it to work. How do I intigrate it with the sub? Do I need to work on the elseif structure. Going to read about that now.
Code:
<script language="VBScript" type="text/vbscript">
Sub btnSubmit_OnClick()

Dim objregx
set objregx = New RegExp
objregx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"

if (objRegx.test(honotice.txthomephone.value)) then
    else
        Alert "You must enter a proper phone number example 305-555-1212. If you do not have a contact number please call 1-800-882-3054"
honotice.txthomephone.focus
    Exit Sub
    End If
    Call honotice.submit()
End Sub

</script>
Thanks!
Rich

Reply With Quote
  #8 (permalink)  
Old July 26th, 2004, 02:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Something like this:

Sub btnSubmit_OnClick()
Dim objregx
set objregx = New RegExp
objregx.pattern = "(1-)?\d{3,}-\d{3,}-\d{4,}"

    If isdate(honotice.txtlossdate.value) = 0 Then
        Alert "You must enter a loss date. If you do not know the loss date please call 1-800-000-0000"
        honotice.txtlossdate.focus
        Exit Sub
    ElseIf Len(honotice.txtinsuredname.value) <2 or IsNumeric(honotice.txtinsuredname.value)then
        Alert "You must enter the insured. If you do not know the insured name please call 1-800-000-0000"
        honotice.txtinsuredname.focus
        Exit Sub
    ElseIf Len(honotice.txtpolicynumber.value) = 0 Then
        Alert "You must enter the policy number. If you do not know the policy number please call 1-800-000-0000"
        honotice.txtpolicynumber.focus
        Exit Sub
    ElseIf isdate(honotice.txtpolicyeffectdate.value) = 0 Then
        Alert "You must enter a policy effective date. If you do not know the policy effective date please call 1-800-000-0000"
        honotice.txtpolicyeffectdate.focus
        Exit Sub

    'Was just if, that is incorrect, and adjusted. By the way, vbscript also doesn't use alert,
    'use msgbox; was alert working for you?
    elseif (objRegx.test(honotice.txthomephone.value)) then
        Alert "You must enter a phone number. If you do not have a phone please call 1-800-000-0000"
        honotice.txthomephone.focus
        Exit Sub
    End If
    Call honotice.submit()
End Sub

Found two errors, look for the comment above. In addition, you can remove the exit subs and put the Call honotice.submit() in the last routine if you want it to run whenever none of the other conditions are met. Just reduces some code for you.

    elseif (objRegx.test(honotice.txthomephone.value)) then
        Alert "You must enter a phone number. If you do not have a phone please call 1-800-000-0000"
        honotice.txthomephone.focus
    else
        Call honotice.submit()
    End If
Reply With Quote
  #9 (permalink)  
Old July 26th, 2004, 02:37 PM
Authorized User
 
Join Date: Jul 2003
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! I'll work on it. Alert did work. Good old IE its forgiving. LOL

Rich

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation in C++ code_lover C++ Programming 3 July 23rd, 2008 01:26 AM
Validation using Validation Framework kalyangvd Struts 1 January 2nd, 2008 06:53 AM
validation dhaval229 ASP.NET 1.0 and 1.1 Professional 2 February 14th, 2006 02:10 PM
Validation question savoym C# 2 July 19th, 2004 07:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.