Solved

replace field value

Posted on 2001-07-24
6
319 Views
Last Modified: 2012-08-13
what I am trying to do is replace a field value and set it to blank if the value isn't a date... I have tried several things without any success.

function validDate(fieldValue,fieldName){
 if (fieldValue.length > 0) {
  var d = fieldValue;
  var fieldName = fieldName;
  d = new Date(d);
  if (isNaN(d))
     alert ('Sorry, not in a valid date format');
     //alert (fieldName)
     fieldName = '';
}
}

where fieldName = document.forms.n_PCOriginalDate.value

thanks
0
Comment
Question by:yolish
6 Comments
 
LVL 2

Accepted Solution

by:
CUTTHEMUSIC earned 25 total points
ID: 6315462
I found this.
http://www.developer.irt.org/script/1.htm
I'll try to put it into a function that corresponds to your question although the link might be a good start.
0
 
LVL 2

Expert Comment

by:CUTTHEMUSIC
ID: 6315490
On other thing can you use client side vbScript (IE Only)?
0
 
LVL 2

Expert Comment

by:CUTTHEMUSIC
ID: 6315499
Here is a script that I found.

/*
   Script:                 dateValidate.js

   Source:                 http://www.shastrynetcom/

   Author:                 Subramanya Shastry Y V H
                                       shastry@email.com

   Description:            Generic Date validation routine.

   Usage:                  Include this file with script tag and pass the date value to be
                           validated to the function 'isProperDate()'

                                       This file can be included with the script tag
                                       <script src="dateValidate.js"></script>

                                       It is my advice that you include this file directly from my
                                       site, so that you get to use the script which is up-to-date.
                                       You can do so with the script tag
                                       <script src="http://www.shastrynet.com/javascript/dateValidate.js"></script>

   Copyright:              This script can be freely used with or without modifications.
 
                           The author cannot be held responsible for any possible mistakes
                           in the coding. It is the responsibility of the user to make
                           sure that the code is working fine and is bug-less.
 
                           This Script cannot be distributed without prior permission from
                           the author.
*/

//------------------------------------------------------------------------------------
// function: isProperDate
//           Function to tell whether the given date is valid or not
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function isProperDate(argDate) {
      var tmpDay = getDay(argDate)
      var tmpMon = getMonth(argDate)
      var tmpYear = getYear(argDate)

      return isProperDay(tmpDay, tmpMon, tmpYear) && isProperMonth(tmpMon) && isProperYear(tmpYear)
}

//------------------------------------------------------------------------------------
// function: isWhiteSpace
//           Function to check whether the given argument consists of charactes other
//           than a space and \t
//------------------------------------------------------------------------------------
function isWhiteSpace(argWhiteSpace) {
      argWs = argWhiteSpace.toString()
      
      for (var intI=0; intI < argWs.length; intI++)
            if (argWs.charAt(intI) != ' ' && argWs.charAt(intI) != '\t')
                  return false
      
      return true
}

//------------------------------------------------------------------------------------
// function: isLeapYear
//           Function to tell, whether the given year is leap year or not
//------------------------------------------------------------------------------------
function isLeapYear(argYear) {
      return ((argYear % 4 == 0) && (argYear % 100 != 0)) || (argYear % 400 == 0)
}

//------------------------------------------------------------------------------------
// function: daysInMonth
//           Function to return the maximum number of days in a given month of a
//           given year
//------------------------------------------------------------------------------------
function daysInMonth(argMonth, argYear) {
      switch (Number(argMonth)) {
            case 1:            // Jan
            case 3:            // Mar
            case 5:            // May
            case 7:            // Jul
            case 8:            // Aug
            case 10:            // Oct
            case 12:            // Dec
                  return 31;
                  break;
            
            case 4:            // Apr
            case 6:            // Jun
            case 9:            // Sep
            case 11:            // Nov
                  return 30;
                  break;
            
            case 2:            // Feb
                  if (isLeapYear(argYear))
                        return 29
                  else
                        return 28
                  break;
            
            default:
                  return 0;
      }
}

//------------------------------------------------------------------------------------
// function: getDateSeparator
//           Function to return the date separator
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getDateSeparator(argDate) {
      // Are there invalid separators?
      if ((argDate.indexOf('-') > 0) && (argDate.indexOf('/') > 0))
            return ' '

      if (argDate.indexOf('-') > 0)
            return '-'
      else
            if (argDate.indexOf('/') > 0)
                  return '/'
            else
                  return ' '
}

//------------------------------------------------------------------------------------
// function: getYear
//           Function to return the year part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getYear(argDate) {
      var dateSep = getDateSeparator(argDate)
      
      if (dateSep == ' ')
            return 0

      if(argDate.split(dateSep).length == 3)
            return argDate.split(dateSep)[2]
      else
            return 0
}

//------------------------------------------------------------------------------------
// function: getMonth
//           Function to return the month part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getMonth(argDate) {
      var dateSep = getDateSeparator(argDate)
      
      if (dateSep == ' ')
            return 0

      if(argDate.split(dateSep).length == 3)
            return argDate.split(dateSep)[0]
      else
            return 0
}

//------------------------------------------------------------------------------------
// function: getDay
//           Function to return the day part of the given date.
//           This function expects date in the format of mm/dd/yyyy or mm/dd/yy
//           or mm-dd-yyyy or mm-dd-yy
//------------------------------------------------------------------------------------
function getDay(argDate) {
      var dateSep = getDateSeparator(argDate)
      
      if (dateSep == ' ')
            return 0

      if(argDate.split(dateSep).length == 3)
            return argDate.split(dateSep)[1]
      else
            return 0
}

//------------------------------------------------------------------------------------
// function: isProperDay
//           Function to tell whether the given day of the given month is valid
//------------------------------------------------------------------------------------
function isProperDay(argDay, argMonth, argYear) {
      if ((isWhiteSpace(argDay)) || (argDay == 0))
            return false

      if ((argDay > 0) && (argDay < daysInMonth(argMonth, argYear) + 1))
            return true
      else
            return false
}

//------------------------------------------------------------------------------------
// function: isProperMonth
//           Function to tell whether the given month is a valid one
//------------------------------------------------------------------------------------
function isProperMonth(argMonth) {
      if ((isWhiteSpace(argMonth)) || (argMonth == 0))
            return false
      
      if ((argMonth > 0) && (argMonth < 13))
            return true
      else
            return false
}

//------------------------------------------------------------------------------------
// function: isProperYear
//           Function to tell whether the given Year is a valid one
//------------------------------------------------------------------------------------
function isProperYear(argYear) {
      if ((isWhiteSpace(argYear)) || (argYear.toString().length > 4) || (argYear.toString().length == 3))
            return false
      
      switch (argYear.toString().length) {
            case 1:
                  if (argYear >=0 && argYear < 10)
                        return true
                  else
                        return false
                  
            case 2:
                  if (argYear >=0 && argYear < 100)
                        return true
                  else
                        return false
                  
            case 4:
                  if (((argYear >=1900) || (argYear >=2000)) && ((argYear < 3000) || (argYear < 2000)))
                        return true
                  else
                        return false
            
            default:
                  return false
      }
}
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 22

Expert Comment

by:CJ_S
ID: 6316271
function isValidDate(dateStr)
{
  var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
  // To require a 4 digit year entry, use this line instead:
  // var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?
  if (matchArray == null)
  {
    // alert("Date is not in a valid format.")
    return false;
  }
  month = matchArray[3]; // parse date into variables
  day = matchArray[1];
  year = matchArray[4];
  if (month < 1 || month > 12)
  { // check month range
    // alert("Month must be between 1 and 12.");
    return false;
  }
  if (day < 1 || day > 31)
  {
    // alert("Day must be between 1 and 31.");
    return false;
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31)
  {
    // alert("Month "+month+" doesn't have 31 days!")
    return false
  }
  if (month == 2)
  { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap))
    {
      // alert("February " + year + " doesn't have " + day + " days!");
      return false;
    }
  }
  return true; // date is valid
}



function validDate(field)
{
   if(!isValidDate(field.value))
   {
       alert ('Sorry, not in a valid date format');
       field.value="";
   }
}

Now call it like:
<input type=text onChange="validDate(this)">

regards,
CJ




if (!isValidDate('30/02/2000')) alert('Invalid date!');
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 7152077
________________________________________
do not accept this comment as the answer

this question has been abandoned. experts please
post recommendations below to help the moderators.


________________________________________
recommendation
split.
CJ
CUTTHEMUSIC
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7181505
Per recommendation, force-accepted.

Netminder
CS Moderator

CJ_S: points for you at http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20328609
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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 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