0

I have a form with some controls. I didn't use validations in visual studio toolbox, In fact I have defined validations for these controls using java script . one of these validations is Regular Expression .
how to check user's data with this regular expression?

for (i = 0; i < rows.length; i++) {
    if (patterns[i] != "") {
        val = document.get ElementById ('cntrl'+i).value;
        ptrn = patterns[i];
        if (!ptrn.test(val)) {
            msg += "bad format. \n";
            j = false;
        }
    }
}

I test this code by Firebug, but it exits from second if loop in first time.

Angel
  • 733
  • 1
  • 7
  • 12
  • 1
    Can we see some code, such as what you've tried, what you want to validate and the _RegExp_ you're trying to use? – Paul S. Jun 25 '13 at 23:11
  • Make sure to validate on the server-side, too. Validating with JavaScript should only be used for the user's convenience; JavaScript can be easily turned off, and you still need to make sure your server is safe. – voithos Jun 25 '13 at 23:11
  • @voithos: how to validate on server side? – Angel Jun 25 '13 at 23:21
  • Just perform the same validation checks after the form submits, in ASP.NET – voithos Jun 25 '13 at 23:24
  • @voithos:before submitting, I check data using javascript. your meaning is that I check them after submitting and by c#? – Angel Jun 25 '13 at 23:28
  • @Angel: [Exactly](http://stackoverflow.com/questions/4076927/why-do-we-need-server-side-as-well-as-client-side-validation-for-web-application). – voithos Jun 25 '13 at 23:29

1 Answers1

0
var pattern = /^\d{3}[-]\d{4}$/;
var input = '867-5309';

if (input.match(pattern))
{
  console.log("Jenny I've got your number.");
}

--output:--
Jenny I've got your number
7stud
  • 46,922
  • 14
  • 101
  • 127