Click here to Skip to main content
15,301,291 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Team,

I have Code as follows for Regular Expression:
On button Click i am verifying the input value.
C#
function validateValues() {

           if ($('#batchIDSelect').val() == '' || $('#batchIDSelect').val() == null) {
               alert("Select the BatchID");
               return false;
           }
           var regExpBatchID = /^[0-9]*$/;
           if (!regExpBatchID.test($('#batchIDSelect').val())) {
               alert("Type the Number in BatchId.No Characters are Allowed");
               return false;
           }

           if ($('#deoIdSelect').val() == '' || $('#deoIdSelect').val() == null) {
               alert("Select the DEO ID");
               return false;
           }
           var regExpDEOID = /^[a-zA-Z0-9]*$/;
           if (!regExpDEOID.test($('#deoIdSelect').val())) {
               alert("DEOID Must Be Number and Alphabets Only.");
               return false;
           }

           return true;
       }


on Button click ,i am Validating as follows:
C#
$("#getCountButton").click(function () {
            debugger;
            if (!validateValues()) {
                return;
            }
         
        });


Now the problem is if it is not validate i cant see the alert message on page.Why it is so??
It gives me the return value for proper validations not for the incorrect validations.I need both validations.
please guide me where i am exactly wrong.
thanks
Harshal.
Posted
Comments
   
What are you trying to achieve?
—SA
R Harshal 4-Apr-14 3:18am
   
I want to do this:
function validateValues() {

if ($('#batchIDSelect').val() == '' || $('#batchIDSelect').val() == null) {
alert("Select the BatchID");
return false;
}
return true
}
$("#getCountButton").click(function () {
debugger;
if (!validateValues()) {
return;
}

});

If the batchid is empty i want to show the message on page ie:"Pls select the batchid "
That i am not getting.
R Harshal 4-Apr-14 3:19am
   
I want to do this:
function validateValues() {

if ($('#batchIDSelect').val() == '' || $('#batchIDSelect').val() == null) {
alert("Select the BatchID");
return false;
}
return true
}
$("#getCountButton").click(function () {
debugger;
if (!validateValues()) {
return;
}

});

If the batchid is empty i want to show the message on page ie:"Pls select the batchid "
That i am not getting.Why is it so.
Thanks
Harshal

1 solution

The problem is not clear at all (see also my comment to the question). However, a formal answer would be very simple: you see alert message on certain condition just because you programmed it to happen; one can clearly see it in your code. :-)

Now, the problem is: it is the utterly bad design when you mix up the validation with some action taken through validation, such as alert. This kind of code is totally unsupportable. You need to isolate different aspects of your code. In this case, a validation function (any validation, with no exclusion) should only calculate some value, without any side effects. In this case, you can return, for example, a string describing the validation problem, or null or empty string, in case of successful validation.
An alternative approach would be throwing some exception with the object describing the validation problem (could also be a sting, in the simple case). And successful validation should do nothing. Please see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw[^].

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900