Solved

Server Side VBScript Form Validation

Posted on 2000-05-02
5
798 Views
Last Modified: 2008-02-20
I need to do some server side validation on a form to make sure I have the best possible set of data.  I'm a little new to VBScript and would like some resources (books, online articles, etc) that can help me.  I need to do stuff like making sure certain fields contain only #'s, stipping out unnecessary formatting (like stripping $100,000.00 to 100000), and validating date format so mm/dd/yyyy is what is received.

Thanks,

Derek.
0
Comment
Question by:dgwest
5 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 2769551
have a look on www.asp101.com.

really for performance reasons you should try to validate form data on the client side using javascript and disallow submission if the data is naf, this will avoid round trips to the server and essentailly of-load some of the cpu work onto the client.

the only time you should validate on the server is when the item of data you are validating must be validated against items of data in the/a server side data store.

for a good help file on vb script with plenty of examples visit this site and download the docuemtation

http://msdn.microsoft.com/scripting/default.htm?/scripting/windowshost/

in there you will fing various conversion routines etc isNumeric type functions..

i think that to test a date you will judt be able to run the data through CDate and then test that it reaturns something . if not its invalid ?

theres tons of javascript examples around the net ..

hope ive helped  
0
 
LVL 1

Expert Comment

by:eladr
ID: 2769690
use javascript for the validation.
you can also use isdate form validating date.
see the msdn:
http://msdn.microsoft.com/scripting
eladr
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 2770959
To strip out characters you can use RegEx in VB5.1 or use the Replace() function.  Say you have a string you want to strip out the , or $ from, you can use something like this;

sString = "$1,000,000"
sString = Replace(sString,",",""(sString,"$",""))

This will return a string that is 1000000.

As far as the date goes, there are a few things you may want to look into, like IsDate(), or striping the from date into individual fields like dd, mm, yyyy then submit them as a joined string;

<%
Function ConvertUS(MyDate)
    Dim MyDay, MyMonth, MyYear
    MyDay = DatePart("d",MyDate)
    MyMonth = DatePart("m",MyDate)
    MyYear = DatePart("yyyy",MyDate)
    ConvertUS = MyMonth & "/" & MyDay & "/" & MyYear
End Function

MyDateVal = ConvertUS(MyDate)
Response.Write MyDateVal
%>


Mark
0
 
LVL 4

Accepted Solution

by:
FRehman earned 150 total points
ID: 2772218
when ever you want to validate the form then always use client side script do'nt use server side script beacuse when user submit the form then it is confirm from the server that if the data is valid and if not then it return back so it take much time
I give you the sample file you see it from it you can also check that if the email is proper format and date is proper formate if you face difficulty in this code the let me know at leo_faisal@yahoo.com/*********************************************************************************
*      FUNCTION:            isBetween
*      PARAMETERS:            val            AS any value
*                              lo            AS Lower limit to check
*                              hi            AS Higher limit to check
*      CALLS:                  NOTHING
*      RETURNS:            TRUE if val is between lo and hi both inclusive, otherwise false.
**********************************************************************************/
function isBetween (val, lo, hi) {
      if ((val < lo) || (val > hi)) { return(false); }
      else { return(true); }
}

/*********************************************************************************
*      FUNCTION:            isDate checks a valid date
*      PARAMETERS:            theStr            AS String
*      CALLS:                  isBetween, isInt
*      RETURNS:            TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isDate (theStr) {
      if (isBetween(theStr.length, 8, 10) == false) { return(false); }
      else {
            var the1st = theStr.indexOf('/');
            var the2nd = theStr.lastIndexOf('/');
            
            if (the1st == the2nd) { return(false); }
            else {
                  var d = theStr.substring(the1st+1,the2nd);
                  var m = theStr.substring(0,the1st);
                  var y = theStr.substring(the2nd+1,theStr.length);
                  var maxDays = 31;

                  if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
                        return(false); }
                  else if (y.length < 4) { return(false); }
                  else if (isBetween (m, 1, 12) == false) { return(false); }
                  else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
                  else if (m==2) {
                        if (y % 4 > 0) maxDays = 28;
                        else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
                  else maxDays = 29;
                  }
                  
                  if (isBetween(d, 1, maxDays) == false) { return(false); }
                  else { return(true); }
            }
      }
      
}


function isDateRange (theStr,focus,errorMessage,lo,hi) {
      if (isBetween(theStr.length, 8, 10) == false)
      {
            alert(errorMessage);
            focus.focus();      
            return(false);
      }
      else {
            var the1st = theStr.indexOf('/');
            var the2nd = theStr.lastIndexOf('/');
            
            if (the1st == the2nd) {

                  alert(errorMessage);
                  focus.focus();      
                  return(      false);
            }
            else
            {
                  var m = theStr.substring(the1st+1,the2nd);
                  var d = theStr.substring(0,the1st);
                  var y = theStr.substring(the2nd+1,theStr.length);
                  var maxDays = 31;

                  if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
                              alert(errorMessage);
                              focus.focus();      
                              return(false); }
                  else if (y.length < 4) {
                              alert(errorMessage);
                              focus.focus();      
                              return(false);
                  }
                  else if (isBetween (m, 1, 12) == false) {
                        alert(errorMessage);
                        focus.focus();      
                        return(false);
                  }
                  else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
                  else if (m==2) {
                        if (y % 4 > 0) maxDays = 28;
                        else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
                  else maxDays = 29;
                  }
                  
                  if (isBetween(d, 1, maxDays) == false) {
                              alert(errorMessage);
                              focus.focus();      
                              return(false);
                  }
                  else {
                        if(!isBetween(y,lo,hi))
                        {
                              alert(errorMessage);
                              focus.focus();      
                              return(false);
                        }
                        else
                        {      return(true); }
                  }
            }
      }
      
}

/*********************************************************************************
*      FUNCTION:            isDigit
*      PARAMETER:            Any Chracter
*      CALLS                  isEmpty to check the NULL
*      RETURNS:            TRUE if the passed chracter is a digit, otherwise FALSE
**********************************************************************************/
function isDigit(theNum) {
      var theMask = '0123456789';
      
      if (isEmpty(theNum)) return(false);
      else if (theMask.indexOf(theNum) == -1) return(false);
      
      return(true);
}
/*********************************************************************************
*      FUNCTION:            isEmail
*      PARAMETER:            String (Email Address)
*      RETURNS:            TRUE if the String is a valid Email address
*                              FALSE if the passed string is not a valid Email Address
*      EMAIL FORMAT:      AnyName@EmailServer e.g; webmaster@hotmail.com
*                              @ sign can appear only once in the email address.
**********************************************************************************/
function isEmail (theStr) {
      var atIndex = theStr.indexOf('@');
      var dotIndex = theStr.indexOf('.', atIndex);
      var flag = true;
      theSub = theStr.substring(0, dotIndex+1)

      if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
      {      flag = false; }
      else { flag = true; }
      
      return(flag);
}

/*********************************************************************************
*      FUNCTION      isEmpty checks if the parameter is empty or null
*      PARAMETER      str            AS String
**********************************************************************************/
function isEmpty (str) {
      if ((str==null)||(str.length==0)) return true;
      else return(false);
}

function isStringEmpty (str,focus,errorMessage) {
      if ((str==null)||(str.length==0))
      {
            alert("Type "+errorMessage+" in "+errorMessage+" field");
            focus.focus();
            return true;
      }
      else return(false);
}

/*********************************************************************************
*      FUNCTION:            isInt
*      PARAMETER:            theStr      AS String
*      RETURNS:            TRUE if the passed parameter is an integer, otherwise FALSE
*      CALLS:                  isDigit
**********************************************************************************/
function isInt (theStr) {
      var flag = true;

      if (isEmpty(theStr)) { flag=false; }
      else
      {      for (var i=0; i<theStr.length; i++) {
                  if (isDigit(theStr.substring(i,i+1)) == false) {
                        flag = false; break;
                  }
            }
      }
      return(flag);
}

/*********************************************************************************
*      FUNCTION:            isReal
*      PARAMETER:            theStr      AS String
                              decLen      AS Integer (how many digits after period)
*      RETURNS:            TRUE if theStr is a float, otherwise FALSE
*      CALLS:                  isInt
**********************************************************************************/
function isReal (theStr, decLen) {
      var dot1st = theStr.indexOf('.');
      var dot2nd = theStr.lastIndexOf('.');
      var OK = true;
      
      if (isEmpty(theStr)) return false;

      if (dot1st == -1) return(true)
      else if (dot1st != dot2nd) return (false);
      else if (dot1st==0) return (false);
      else {
            var intPart = theStr.substring(0, dot1st);
            var decPart = theStr.substring(dot2nd+1);

            if (decPart.length > decLen) return(false);
            else if (!isInt(intPart) || !isInt(decPart)) return (false);
            else if (isEmpty(decPart)) return (false);
            else return(true);
      }
}

/*********************************************************************************
*      FUNCTION:            isEmail
*      PARAMETER:            String (Email Address)
*      RETURNS:            TRUE if the String is a valid Email address
*                              FALSE if the passed string is not a valid Email Address
*      EMAIL FORMAT:      AnyName@EmailServer e.g; webmaster@hotmail.com
*                              @ sign can appear only once in the email address.
*********************************************************************************/
function isEmail (theStr) {
      var atIndex = theStr.indexOf('@');
      var dotIndex = theStr.indexOf('.', atIndex);
      var flag = true;
      theSub = theStr.substring(0, dotIndex+1)

      if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
      {      return(false); }
      else { return(true); }
}
/*********************************************************************************
*      FUNCTION:            showHelp
*      DESCRIPTION:      Opens a document in a new window.                  
*      PARAMETERS:            docHelp       Help document
*                              winHeight      Height of new window
*                              winWidth      Width of the new window.
*      CALLS:                  NOTHING
*      RETURNS:            NOTHING
**********************************************************************************/
function showHelp (docHelp, winHeight, winWidth) {
      var winName="winHelp";
      var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1";
      opt+=",resizable=0,";
      opt+=("width="+winWidth+",height="+winHeight);
      newWin = window.open(docHelp,winName,opt);
}
/*********************************************************************************
*      FUNCTION:            showWindow
*      DESCRIPTION:      Opens a document in a new window.                  
*      PARAMETERS:            doc             Document
*      CALLS:                  NOTHING
*      RETURNS:            NOTHING
**********************************************************************************/
function showWindow (doc) {
      var winName="win";
      var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1";
      newWin = window.open(doc);
}
/*********************************************************************************
*      EO_JSLib.js
**********************************************************************************/

0
 

Author Comment

by:dgwest
ID: 2777070
Adjusted points from 75 to 150
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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