hunt hunt - 5 months ago 11
Javascript Question

How to validate all empty text once in .NET

function save() {

var iframeID = $('iframe').attr('id');

var code = lazyMethod_get(iframeID, "form1", "txt_code");
var loa = lazyMethod_get(iframeID, "form1", "txt_loa");
var DWT = lazyMethod_get(iframeID, "form1", "txt_dwt");
var GrossTonnage = lazyMethod_get(iframeID, "form1", "txt_gross");

if (code == '')
{
Ext.Msg.alert("Code need to be fill in!");
}
else if (loa == '')
{
Ext.Msg.alert("LOA need to be fill in!");
}
else if (DWT == '')
{
Ext.Msg.alert("DWT need to be fill in!");
}
else if (GrossTonnage == '')
{
Ext.Msg.alert("Gross Tonnage need to be fill in!");
}
else
{
validateDuplicate(name);
this.up('window').close();
}
}


Above is the code i done and it prompt one by one. I need it to validate just once if the user miss out 2 of the textbox it will prompt the error once and not two times.

Answer

Instead of alert in every condition you can append error in variable and then alert ones.

var errors=''; 
if (code == '') {

    errors+="Code need to be fill in!";
}   
if (loa == '') {

    errors+="LOA need to be fill in!";
}

After all condition check if any error and then alert

if(errors.length>0){
    Ext.Msg.alert(errors);
}

This is javascript example. same can be applied for C# also