Solved

Date validation - no empty and null input allowed, How?

Posted on 2007-03-20
5
297 Views
Last Modified: 2008-02-01
I have the following JS

<script language = "Javascript">
......
function ValidateForm(){
      var dt=document.NetFlow.BegDate
        var dt2=document.NetFlow.EndDate
        var dt3= document.NetFlow.Num_Results
         
      if (isDate(dt.value)==false)
        {
            dt.focus()
            return false
         }
         else if (isDate(dt2.value)==false)
         {
                dt2.focus()
            return false
      }
       
       else if(dt3.value.match(/[^0-9]/))
        {
        alert("Invalid data format.\nOnly numbers are allowed.");
        dt3.focus()
        return false
        }
    return true
 }
 
 
 function ValidateIntlNumber(fld) {
     if (fld.value.match(/[^0-9]/)) {
     
          alert ('Only numbers are permitted');
          // Un comment the next line is you want to remove the non-numbers automatically
          fld.value=fld.value.replace(/[^0-9]/g,'');
     }
}
</script>

........

                    <label>Degree of Imbalence: <input name="DegImb" type="text" size="3" value="65" onkeyup="ValidateIntlNumber(this);"> ( enter as 50% )&nbsp; &nbsp; </label>
                    <label>Total Flows:<input name="TotalFlowImb" type="text" size="3" value="10" onkeyup="ValidateIntlNumber(this);" > ( enter as  )</label>
               
 <label># of Results: <input type="text" name="Num_Results" size="3" maxlength="3" value="25" onkeyup="ValidateIntlNumber(this);" ></label>
           
 <label>Date Range: <input type="text"  size="20" maxlength="30"  name= "BegDate" value="<%=y%>" > - <input type="text" size="20" maxlength="30" name="EndDate" value=<%=s%> > ( enter as mm/dd/yyyy)</label>
        </div>

.......

I have one question to improve my code
I don't want null value or empty value be stayed in the any fields above. That means user must input a valid data into the field, can not leave null, blank before he submits the form. How to do that?
0
Comment
Question by:wasabi3689
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 25 total points
ID: 18762011
something like this..

function trim(s){
      return s.replace(/^\s*/,'').replace(/\s*$/, '');
}

function ValidateForm(){
      var dt=document.NetFlow.BegDate
        var dt2=document.NetFlow.EndDate
        var dt3= document.NetFlow.Num_Results
         
if (trim(dt.value) == "") {
    alert("Please enter a value");
    dt.focus();dt.select();
    return false;
}

      if (isDate(dt.value)==false)
        {
            dt.focus()
            return false
         }


if (trim(dt2.value) == "") {
    alert("Please enter a value");
    dt2.focus();dt2.select();
    return false;
}

         if (isDate(dt2.value)==false)
         {
                dt2.focus()
            return false
      }
       

if (trim(dt3.value) == "") {
    alert("Please enter a value");
    dt3.focus();dt3.select();
    return false;
}

       if(dt3.value.match(/[^0-9]/))
        {
        alert("Invalid data format.\nOnly numbers are allowed.");
        dt3.focus()
        return false
        }
    return true
 }
0
 
LVL 10

Assisted Solution

by:Jaax
Jaax earned 25 total points
ID: 18762100
Call the function check upon Submit

function fnCheck(){
  for(i=0; i<document.forms[0].elements.length; i++){
     ele = document.forms[0].elements[i];
       if(ele.type == "text"){
          rep = trim(ele.value);
          alert("Rep : "+rep+  "$$" );
          if(rep.length == 0){
            alert("Invalid Value : ");
            ele.focus();
          }
       }

  }

}

function trim(str){

  if(str.length > 0){
        while(str.charAt(0) == ' '){
           if(str.length >= 1){
               str = str.substring(1);
             }else{
               str = "";
             }
        }
  }

  if(str.length > 0){
        while(str.charAt(str.length-1) == ' '){
        if(str.length >= 1){
               str = str.substring(0, str.length);
             }else{
               str = "";
             }
        }
  }

  return str;

}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18774821
Is your server side Java? You can also use Struts validator framework. Or .NET validator controls in case of ASP .NET. However, to realize the advantage of distributed client-server processing, it might be better to do it at the client side itself using the code already provided.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

751 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