How to check a form value with jQuery, if you use $(this)?

0 like 0 dislike
4 views
There is the following code:
HTML:
Write about your question :)

JS ( jQuery ):
$('input').on('input', function() { if ( $(this).attr('name') === 'name' ) { if ( $(this).val().length <= 2) { $('.name-spoiler').text('Your name must contain at least 3 letters!').css('color', 'red'); } else { $('.name-spoiler').text('Your name is correct!').css('color', 'green'); } } if ( $(this).attr('name') === 'email' ) { if ( !$(this + ':contains("@")') ) { $('.email-spoiler').text('Your email must contain "@" character!').css('color', 'red'); } else { $('.email-spoiler').text('Your email is correct!').css('color', 'green'); } } });


The catch is that you have to use :contains() in conjunction with $(this), jQuery considers it as trouble, ie it is contrary to its structure. What about indexOf(): it does not work with jQuery functions like $(this).text (), etc. How to check $(this).text() or $(this) on the content of the symbol, for example "@" for email validation? Can and regular expressions, but more appropriate jQuery methods. In advance thank you from the heart :)))
by | 4 views

2 Answers

0 like 0 dislike
indexOf(): it does not work with jQuery functions like $(this).text()

Strange things You tell, everything works. But in your case it is necessary not to take the text and value.
if($(this).val().indexOf("@") > 0) { ... }
by
0 like 0 dislike
Hedgehogs were pricked, cried, but continued to guzzle a cactus.


It's about you. Take any library validation (e.g. www.formvalidator.net ) and don't do crap...

PS
Just threw on one site in a single sheet of bad code, after an hour of trying to understand what the author meant, and connecting the library cost writing literally ten lines of code for a huge form..
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
33,949 users