Avoid repeating parts of a jquery script -


I am trying to check some input-fields before submitting a form. But this code looks a bit complicated for me as there are repeating parts, I think it is possible to customize / hesitate the code.

What is done by the code:

  1. Check input
  2. If there is an error, then a class has been added
  3. If there is an error category -> the form will not be submitted
  4. I tried to keep some parts in a different function, but I did not find it :-( I think a different function would be better i.e. if I had two different input fields For the same case ...

You can create a function to save a lot of code that check Submit (function) {$ ("tr"). Each (function () {var self = $ (this); Validate (self) ), 1, / ^ \ d + (-? \ D + | \ * * $ /, true Validated ATD (self, 2, / ^ \ d + (\. \ D + | \ * * * ($? \ D + (\. \ D + | \ * *) | \ d *) $ /, truth); Manyatteediti (self, 3 / ^ [A-GAA -jim] + $ / i);}); if (! $ ( '. error ") length) {event.preventDefault ();}}); function ValidateTD (TR, numbers, patterns, removeSpacesAndReplaceCommasWithDot) {if (RemoveSpacesAndReplaceCommasWithDot undefined ==) {removeSpacesAndReplaceCommasWithDot = false;} $ .Each (tr.children ( "Td: eq (" + + + + ")"), function ( ) {If (removeSpace SAndReplaceCommasWithDot) {var str = $ (this). Children ('Input'). Val (). Replace (/ / g, ""). Replace (/, / g, "."); . $ (This) .children ('Input') Val (STR); } And {var str = $ (this). Children ('Input'). Val (); } If (str.match (pattern)) {$ (this). Children ('input'). RemoveClass ('error'); } And {$ (this). Children ('Input'). AddClass ('Error'); }}); }


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -