Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

I got this code it works but how can I make null value fine

Posted on 2004-04-05
4
Medium Priority
?
196 Views
Last Modified: 2008-02-01
I got this code I implemented it to the website. How do I make null values okay.

Check this code...

<SCRIPT LANGUAGE="JavaScript">

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
     var i;
    for (i = 0; i < s.length; i++){  
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
     var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){  
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
     // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
     for (var i = 1; i <= n; i++) {
          this[i] = 31
          if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
          if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
     var daysInMonth = DaysArray(12)
     var pos1=dtStr.indexOf(dtCh)
     var pos2=dtStr.indexOf(dtCh,pos1+1)
     var strMonth=dtStr.substring(0,pos1)
     var strDay=dtStr.substring(pos1+1,pos2)
     var strYear=dtStr.substring(pos2+1)
     strYr=strYear
     if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
     if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
     for (var i = 1; i <= 3; i++) {
          if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
     }
     month=parseInt(strMonth)
     day=parseInt(strDay)
     year=parseInt(strYr)
     if (pos1==-1 || pos2==-1){
          alert("The date format should be : mm/dd/yyyy")
          return false
     }
     if (strMonth.length<1 || month<1 || month>12){
          alert("Please enter a valid month")
          return false
     }
     if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
          alert("Please enter a valid day")
          return false
     }
     if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
          alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
          return false
     }
     if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
          alert("Please enter a valid date")
          return false
     }
return true
}

function ValidateForm(dt){
     //var dt=document.frmSample.txtDate
     if (isDate(dt.value)==false){
          dt.focus()
          return false
     }
    return true
 }


function validatenum(numfield)
{
     if (isNaN(numfield.value))
     {
          alert("Not a number");
     }
}
//-->
</SCRIPT>


<form name="frmSample" method="post" action="" onSubmit="return ValidateForm(txtDate)">
                <p>Enter a Date <font color="#CC0000"><b>(mm/dd/yyyy)</b></font>
                  :
                  <input type="text" name="txtDate" maxlength="10" size="15">
                </p>
                <p>
                  <input type="submit" name="Submit" value="Submit">
                </p>
              </form>

<form method="POST" action="ll.asp">
  <input type="text" name="T1" value="" size="20" onblur="ValidateForm(T1)">
  <input type="text" name="T2" value="" size="20" onblur="validatenum(T2)">
  <input type="submit" value="Submit" name="B1">
  <input type="reset" value="Reset" name="B2"></p>
</form>
0
Comment
Question by:eteran
  • 2
4 Comments
 
LVL 7

Expert Comment

by:brgivens
ID: 10762688
<form method="POST" action="ll.asp">
  <input type="text" name="T1" onblur="if(isNaN(new Date(this.value))){alert('Date is invalid.');this.focus();}" size="20"><input type="text" name="T2" onblur="if(!/^\d+(\.\d+)?$/.test(this.value)){alert('Number is invalid.');this.focus();}" size="20">
  <input type="submit" value="Submit" name="B1">
  <input type="reset" value="Reset" name="B2"></p>
</form>

:)
0
 
LVL 12

Accepted Solution

by:
lil_puffball earned 2000 total points
ID: 10762698
Hi eteran,

What do you mean by "null" values? Do you mean just if it's empty? If so, you can put this where the form is:

<form method="POST" action="ll.asp">
  <input type="text" name="T1" value="" size="20" onblur="if(this.value!=''){ValidateForm(T1);}">
  <input type="text" name="T2" value="" size="20" onblur="if(this.value!=''){validatenum(T2);}">
  <input type="submit" value="Submit" name="B1">
  <input type="reset" value="Reset" name="B2"></p>
</form>
0
 

Author Comment

by:eteran
ID: 10762913
Well, if they put something let it validate it if they don't leave it alone
0
 
LVL 7

Expert Comment

by:brgivens
ID: 10762975
<form method="POST" action="ll.asp">
  <input type="text" name="T1" onblur="if(this.vaue!="" && isNaN(new Date(this.value))){alert('Date is invalid.');this.focus();}" size="20"><input type="text" name="T2" onblur="if(this.value!="" && !/^\d+(\.\d+)?$/.test(this.value)){alert('Number is invalid.');this.focus();}" size="20">
  <input type="submit" value="Submit" name="B1">
  <input type="reset" value="Reset" name="B2"></p>
</form>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

572 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