Hello everyone, I am trying to get a code that will check the length of the a 'key' that will be generated in xxxxx-xxxxx-xxxxx-xx format. I need it to verify that it meets the requirements. I do not need it to validate on the server, but i do need it for my registration page. I have been looking at the web for examples, but the best i can see is using the $("#formname").validate(); but need it for just the kkey section.

Any help would be great! Thanks

3 Years
Discussion Span
Last Post by JorgeM

When you say length, you mean the number of characters? if so, you can simply create a variable in javascript and assign teh "key" to this string, then just check its length. yourstring.length

if (!yourstring.length == someNumber) {
    // its not the correct length, do something...

Edited by JorgeM


The key is going to have dashes, and 20 characters long.


if(!regkey.length == 20(plus dashes?) {
 alert(This key is not the required length);

Like that?


20 includes all characters in that string, including dashes. Sorry, made a minor mistake with my if condition. should be if (length != 20) { ... }

check this example. http://jsfiddle.net/X9px7/

keep in mind, this is just a very simple example. You can use Regular Expressions (regex) if you really want to apply specific rules to what is allowed to be entered. For example, you can write a rule to make sure that a certain format is followed, i.e. 5 numbers, then a dash, then 5 numbers, another dash, another 5 numberss, another dash, then two numbers.

Edited by JorgeM

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.