0

In my website, I'm receiving some subscriptions spam. All of them is from people which type their names using Russian alphabet.

I know that in this case the most recommended thing to do is put a captcha in my subscription page form. But, as this case is so specific, and at the same time I don't want to affect my real customers experience, in a first time I would like to have a jQuery script which deny the form submit when detects Russian alphabet in some input. Can some one help me with it? (Or is better do it using PHP?)

Simple form exemple

<form action="#" id="form-validate">
  <input type="text" class="input" id="firstname">
  <button class="submit">teste</button>
</form>  

To be more specific. For example, a script that detects a Cyrillic character in the input string, such as: "John Потоцкая Doe", and then avoid the form submit.

Thanks a lot in advance.

user3130064
  • 1,071
  • 2
  • 13
  • 22
  • Please provide the community with a minimal, verifiable example (the relevant part of your code) in the body of your question in order for us to assist you better. – esqew Aug 29 '18 at 19:31
  • 1
    @esqew, for exemple in this simple form: https://codepen.io/anon/pen/EegzQJ - I would like a jQuery script that detects if in the input has some Cyrillic characters, for exemple: "John Потоцкая Doe" And if has, block on form submit. Got it? – user3130064 Aug 29 '18 at 19:54
  • It's not a good idea to detect spam on the client-side in browser. A human may switch Javascript off and submit a form. Moreover spam bots often skip Javascript at all, they will simply not notice your efforts. – Alexander Azarov Oct 02 '18 at 12:48

2 Answers2

3

I'd use a Regex range that hits all the Cyrillic unicode ranges:

let pattern  /[\u0400-\u04FF]/;
if (pattern.test("John Потоцкая Doe")) console.log("cyrillic");
else console.log("not cyrillic");

Relevant SO Question

Your code will end up looking like this:

let pattern = /[\u0400-\u04FF]/;

$(document).ready(function() {
  $('#form-validate').submit(function() {
    if (pattern.test($('input#firstname').val())) { //If "input" contains a Cyrillic character...
      alert('Invalid input: please use Latin characters only.'); // pop alert message
      $('input#firstname').val("") // empty field of invalid contents
      return false; // prevent form from submitting
    } else
      return true; // allow form to be submitted
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#" id="form-validate">
  <input type="text" class="input" id="firstname">
  <button class="submit">teste</button>
</form>
esqew
  • 42,425
  • 27
  • 92
  • 132
1

You could see where Cyrillic characters live in the Unicode space here:

https://en.wikipedia.org/wiki/Cyrillic_script_in_Unicode

Then loop through character by character to see if any character falls in that range, something like:

let str = 'Україна';
let cyrillic = false;
for (let i = 0; i < str.length; i++) {
  if (str.codePointAt(i) >= 0x0400 && str.codePointAt(i) <= 0x052f) {
    cyrillic = true;
  }
}

There may be an easier way with regexes, e.g. range /[A-Za-z]/ gives Latin letters, may be something similar for general Unicode code points.

Edit

This might be slightly cleaner with ES5 Array prototype methods, e.g. one-liner would be:

let cyrillic = str.split('').some(char => char.codePointAt(0) >= 0x0400 && char.codePointAt(0) <= 0x052f);
jpsimons
  • 27,382
  • 3
  • 35
  • 45
  • 1
    Hi, @jpsimons. Thanks for your response. So, in this case, how this snippet would avoid a form submit which has, for exemple, the follow string: "John Потоцкая Doe"? https://codepen.io/anon/pen/EegzQJ - Thanks a lot in advance – user3130064 Aug 29 '18 at 19:59
  • Maybe this base could be better to we work on: https://codepen.io/anon/pen/NLbWPz – user3130064 Aug 29 '18 at 20:26