0

I found these javascript validation codes:

<script type="text/javascript">
function validasi_input(form){
   pola_username=/^[a-zA-Z0-9\_\-]{6,100}$/;
   if (!pola_username.test(form.username.value)){
      alert ('Username minimal 6 karakter dan hanya boleh Huruf atau Angka!');
      form.username.focus();
      return false;
   }
return (true);
}
</script>

I want to ask about this part:

pola_username=/^[a-zA-Z0-9\_\-]{6,100}$/;

does anyone can tell me how to understand this kind of format? is it format for letter, or number, or characters?

2
  • This called Regular Expression. You should search more about this keyword. Commented Jun 24, 2014 at 2:55
  • okay. thank yo so much Commented Jun 24, 2014 at 3:00

4 Answers 4

1
/^[a-zA-Z0-9\_\-]{6,100}$/;

In english this means: that a string can have any letter either uppercase or lowercase, numbers, underscores, and hyphens. A minimum length of 6 characters, and a maximum length of 100.

Further details:

The string must start with either a letter, number, underscore, or hyphen.

Sign up to request clarification or add additional context in comments.

1 Comment

@user3740978 anytime I'm always here to help.
0
/^[a-zA-Z0-9\_\-]{6,100}$/

^ asserts that we are at the beginning of the string

[a-zA-Z0-9_-] string can have any letter either uppercase , lowercase, numbers, underscores, or hyphens.

{6,100} matches a length of character having from 6 to 100

$ asserts that we are at the end of the string

Comments

0

Various RegEx explanation/testing tools.

1. Explain RegEx

2. RegEx101

3. Debuggex Demo

^[a-zA-Z0-9\_\-]{6,100}$

Regular expression visualization

Comments

0

^ is an anchor. It asserts position at start of the string

[a-zA-Z0-9\_\-]{6,100} match a single character present in the list below

{6, 100}: Between 6 and 100 times, as many times as possible, giving back as needed

a-z a single character in the range between a and z (case sensitive)

A-Z a single character in the range between A and Z (case sensitive)

0-9 a single character in the range between 0 and 9

\_ matches the character _ literally

\- matches the character - literally

$ is an anchor. It asserts position at end of the string.

An alternative regex using flags would be:

/^[a-z\d\_\-]{6,100}$/i

Here \d matches digits (0-9), and flag i denotes case insensitivity.

This is what regular expressions do to perform matches, for starters:

alt text
(source: gyazo.com)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.