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

Recommended Answers

All 3 Replies

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...

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.