Click here to Skip to main content
15,887,856 members
Home / Discussions / JavaScript
   

JavaScript

 
AnswerRe: disable a onclick function, and enable it Pin
Sibeesh KV24-Sep-14 1:01
professionalSibeesh KV24-Sep-14 1:01 
QuestionParse value from jquery to php and truncate mysql table Pin
Member 1094998617-Jul-14 7:32
Member 1094998617-Jul-14 7:32 
SuggestionRe: Parse value from jquery to php and truncate mysql table Pin
ZurdoDev29-Jul-14 10:13
professionalZurdoDev29-Jul-14 10:13 
Questionweb application for marathi input Pin
Member 104570339-Jul-14 2:37
professionalMember 104570339-Jul-14 2:37 
AnswerRe: web application for marathi input Pin
Graham Breach9-Jul-14 23:31
Graham Breach9-Jul-14 23:31 
AnswerRe: web application for marathi input Pin
Ravindra Bisen18-Jul-14 0:29
Ravindra Bisen18-Jul-14 0:29 
QuestionDate and time validation are not working. Any ideas? <Resolved> Pin
samflex5-Jul-14 12:54
samflex5-Jul-14 12:54 
AnswerRe: Date and time validation are not working. Any ideas? Pin
Richard Deeming7-Jul-14 3:02
mveRichard Deeming7-Jul-14 3:02 
Since you now have multiple rows, you won't have an element with the ID startHour / startMinutes / etc. - they'll all have a row prefix to ensure that they get a unique ID.

Your current validation method won't actually do any validation. It will display an alert, but then allow the post to continue anyway.

Also, you probably don't want an alert for every row with an error. It would be better to keep the UI consistent by using a proper validator control. In this case, the CustomValidator control[^] is probably the best choice.

ASP.NET
<asp:TemplateField HeaderText="End Time">
    <ItemTemplate>
        <asp:DropDownList id="endHour" runat="server">
            ...
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
            ControlToValidate="endHour"
            ErrorMessage="*"
            SetFocusOnError="True"
        />
        <asp:CustomValidator ID="CustomValidator1" runat="server"
            ControlToValidate="endHour"
            ErrorMessage="A minimum of four hours is required."
            Text="*"
            SetFocusOnError="True"
            OnServerValidate="ValidateDuration"
            ClientValidationFunction="validateDuration"
        />
    </ItemTemplate>
</asp:TemplateField>


C#
protected void ValidateDuration(object sender, ServerValidateEventArgs args)
{
    Control validator = (Control)sender;
    Control row = validator.NamingContainer;
    
    int startHour = int.Parse(((DropDownList)row.FindControl("startHour")).SelectedValue);
    int startMinutes = int.Parse(((DropDownList)row.FindControl("startMinutes")).SelectedValue);
    string startAmPm = ((DropDownList)row.FindControl("startAmPm")).SelectedValue;
    
    switch (startAmPm)
    {
        case "AM":
        {
            if (startHour == 12)
            {
                startHour = 0;
            }
            break;
        }
        case "PM":
        {
            if (startHour != 12)
            {
                startHour += 12;
            }
            break;
        }
        default:
        {
            args.IsValid = true;
            return;
        }
    }
    
    int endHour = int.Parse(((DropDownList)row.FindControl("endHour")).SelectedValue);
    int endMinutes = int.Parse(((DropDownList)row.FindControl("endMinutes")).SelectedValue);
    string endAmPm = ((DropDownList)row.FindControl("endAmPm")).SelectedValue;
    
    switch (endAmPm)
    {
        case "AM":
        {
            if (endHour == 12)
            {
                endHour = 0;
            }
            break;
        }
        case "PM":
        {
            if (endHour != 12)
            {
                endHour += 12;
            }
            break;
        }
        default:
        {
            args.IsValid = true;
            return;
        }
    }
    
    int hourDiff = endHour - startHour;
    if (endMinutes < startMinutes)
    {
        hourDiff--;
    }
    
    args.IsValid = hourDiff >= 4;
} 


JavaScript
function validateDuration(sender, args){

    var row = $(sender).closest("tr");
    
    var startHour = parseInt(row.find("select[name$=startHour]").val(), 10);
    var startMinutes = parseInt(row.find("select[name$=startMinutes]").val(), 10);
    var startAmPm = row.find("select[name$=startAmPm]").val();
    
    switch (startAmPm) {
        case "AM": {
            if (startHour === 12) {
                startHour = 0;
            }
            break;
        }
        case "PM": {
            if (startHour !== 12) {
                startHour += 12;
            }
            break;
        }
        default: {
            args.IsValid = true;
            return;
        }
    }
    
    var endHour = parseInt(row.find("select[name$=endHour]").val(), 10);
    var endMinutes = parseInt(row.find("select[name$=endMinutes]").val(), 10);
    var endAmPm = row.find("select[name$=endAmPm]").val();
    
    switch (endAmPm) {
        case "AM": {
            if (endHour === 12) {
                endHour = 0;
            }
            break;
        }
        case "PM": {
            if (endHour !== 12) {
                endHour += 12;
            }
            break;
        }
        default: {
            args.IsValid = true;
            return;
        }
    }
    
    int hourDiff = endHour - startHour;
    if (endMinutes < startMinutes) { 
        hourDiff--; 
    }
    
    args.IsValid = hourDiff >= 4;
}


NB: Your original validation function would consider 1:45 PM to 2:00 PM as one hour. This version will take the minutes into account as well. If that's not what you want, then you can remove the entire if (endMinutes < startMinutes) { ... } block, and the code which retrieves the startMinutes and endMinutes values.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Date and time validation are not working. Any ideas? Pin
samflex7-Jul-14 4:21
samflex7-Jul-14 4:21 
GeneralRe: Date and time validation are not working. Any ideas? Pin
Richard Deeming7-Jul-14 4:27
mveRichard Deeming7-Jul-14 4:27 
GeneralRe: Date and time validation are not working. Any ideas? Pin
samflex7-Jul-14 4:36
samflex7-Jul-14 4:36 
QuestionRe: Date and time validation are not working. Any ideas? Pin
Richard Deeming7-Jul-14 4:45
mveRichard Deeming7-Jul-14 4:45 
AnswerRe: Date and time validation are not working. Any ideas? Pin
samflex7-Jul-14 4:51
samflex7-Jul-14 4:51 
GeneralRe: Date and time validation are not working. Any ideas? Pin
Richard Deeming7-Jul-14 5:17
mveRichard Deeming7-Jul-14 5:17 
GeneralRe: Date and time validation are not working. Any ideas? Pin
samflex7-Jul-14 5:31
samflex7-Jul-14 5:31 
AnswerRe: Date and time validation are not working. Any ideas? Pin
Kornfeld Eliyahu Peter7-Jul-14 3:31
professionalKornfeld Eliyahu Peter7-Jul-14 3:31 
Questionhow to check if user is fan of a facebook page using javascript? Pin
Meax3-Jul-14 6:50
Meax3-Jul-14 6:50 
AnswerRe: how to check if user is fan of a facebook page using javascript? Pin
zeGeek9-Jul-14 14:28
zeGeek9-Jul-14 14:28 
AnswerRe: how to check if user is fan of a facebook page using javascript? Pin
Vfleitao14-Jul-14 22:27
Vfleitao14-Jul-14 22:27 
Questionauto refresh marker on google map with directions Pin
popsompong1-Jul-14 0:10
popsompong1-Jul-14 0:10 
QuestionAngular JS Pin
chauhanvatsal30-Jun-14 20:40
chauhanvatsal30-Jun-14 20:40 
AnswerRe: Angular JS Pin
Kornfeld Eliyahu Peter30-Jun-14 21:41
professionalKornfeld Eliyahu Peter30-Jun-14 21:41 
AnswerRe: Angular JS Pin
gaupoit3-Jul-14 5:27
gaupoit3-Jul-14 5:27 
Questionhow to get data from json url ? Pin
Tamil Purushothaman29-Jun-14 20:43
Tamil Purushothaman29-Jun-14 20:43 
AnswerRe: how to get data from json url ? Pin
Graham Breach29-Jun-14 22:18
Graham Breach29-Jun-14 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.