2 Replies - 265 Views - Last Post: 12 December 2011 - 08:46 PM

#1 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Multiple Regular Expression Validation

Posted 11 December 2011 - 08:47 AM

I have a couple of different forms that the user will go through in steps and I also have multiple regular expressions to check input values against. This is where I'm at now (pseudo):

function myValidator(value) {
  var email = /pattern/;
  var alphabetic = /pattern/;
  var alphanumeric = /pattern/;
  var currency = /pattern/;
  var number = /pattern/;

  if(email.test(value))
    do something
  else
    alert("invalid...");

  if(alphabetic.test(value))
    do something
  else
    alert("invalid..");
  
  etc...

}



My question is how do I check all the values individually by calling the function for the submit button? It seems kind of crazy to do an onclick(myValidator(this.form.inputName.value)) for each one. Suggestions on a better way to go about this?

Thanks in advance.

This post has been edited by ninechances: 11 December 2011 - 08:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple Regular Expression Validation

#2 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • Posts: 555
  • Joined: 30-October 10

Re: Multiple Regular Expression Validation

Posted 11 December 2011 - 09:34 AM

You register an event handler for the onsubmit/submit event that evaluates the contents of your input fields with the test() method.

I would recommend using a string to hold your error messages and simply append all of the error messages that are found during the testing. If the string's still empty (if(errorMessages === "")), after you've validated the input, then the input is valid and it can be sent to the server for further validation. Otherwise your validation function can return false, thus preventing the invalid data from needlessly being submitted to your server, and display the error messages to the user.

Also, here's a useful resource for more information on this topic. :)
Was This Post Helpful? 1
  • +
  • -

#3 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Multiple Regular Expression Validation

Posted 12 December 2011 - 08:46 PM

You can place your regexes in groups.

var echeck = string.match(/(regex1)(regex2)(regex3)(so on...)/g);
//string here means dom walk to your form field.


Make some messages.
var message = [message1, message2, message3, so on...];


You can then loop through these to find the first empty group.
function test_email(group, m){
var a_group = 1;
while(a_group <= group.length){
   //do nothing
   if(group[a_group] == NULL||undefined||""||some_other_default_value){
       return m[a_group-1];
   }
   if(a_group == group.length){
       return var allow = "value_that_allows_post";
   }
   a_group+1;
}
test_email(echeck, message);
// Place posting code allowance here.


Or something like that.

Don't know if the code above will work. Just threw it up. :bs:
The submit events would have to access the test_email function and not allow actual posts until the loop was finished instead of just calling it all on it's lonesome.

This post has been edited by hiddenghost: 12 December 2011 - 09:19 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1