'javascript validate SSID and WPA/WPA2

I am trying to get 2 functions to validate an SSID and WPA2 passcode.

function isValidSSID(ssid) {
    return (regex)
}

and

function isValidWPA(passcode) {
    return (regex)
}

I was hoping to find a regex for each...

I was looking for what are valid characters for each:

The SSID can consist of up to 32 alphanumeric, case-sensitive, characters. The first character cannot be the !, #, or ; character. The +, ], /, ", TAB, and trailing spaces are invalid characters for SSIDs.

WPA: https://superuser.com/questions/223513/what-are-the-technical-requirements-for-a-wpa-psk-passphrase

Thanks, Don

Update:

the SSID function that worked for me:

function isValidSSID(str) { return /^[!#;].|[+\[\]/"\t\s].*$/.test(str); }

I used the site https://regex101.com/r/ddZ9zc/2/

the WPA function that worked for me:

 function isValidWPA(str) { return /^[\u0020-\u007e\u00a0-\u00ff]*$/.test(str); }

Regular expression for all printable characters in JavaScript

I did the length check elsewhere in javascript.

Thanks!



Solution 1:[1]

I use this regex to match SSID:

^[^!#;+\]\/"\t][^+\]\/"\t]{0,30}[^ !#;+\]\/"\t]$|^[^ !#;+\]\/"\t]$

Solution 2:[2]

I used andrey's answer to come up with this:

^[^!#;+\]\/"\t][^+\]\/"\t]{0,30}[^ +\]\/"\t]$|^[^ !#;+\]\/"\t]$[ \t]+$

The SSID can consist of up to 32 alphanumeric, case-sensitive, characters. The first character cannot be the !, #, or ; character. The +, ], /, ", TAB, and trailing spaces are invalid characters for SSIDs.

With the regex andrey provided you could not use !, # or ; in the string at all however the text above specifies that they can't be used only at the start of the string.

Solution 3:[3]

For SSID I would use this regex (excluding the case of zero-length):

/^[^!#;+\]/"\t][^+\]/"\t]{0,31}$/

For WPA passphrase (8 to 63 printable ASCII characters, with encoding ranging from 32 to 126):

/^[\u0020-\u007e]{8,63}$/

Solution 4:[4]

Just a slight improvement on Sparers excellent version, I noticed while making something for a freeradius service, it can match over newlines when it is inserted into grouping... This version, although with other drawbacks, cannot:

([^!#;+\]\/"\t\n][^+\]\/"\t\n]{0,30}[^ +\]\/"\t\n]|^[^ !#;+\]\/"\t\n][ \t]+)$

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andrey
Solution 2
Solution 3 Emanuele
Solution 4 Skunky