4

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!

Don E.
  • 71
  • 1
  • 5

5 Answers5

5

I use this regex to match SSID:

^[^!#;+\]\/"\t][^+\]\/"\t]{0,30}[^ !#;+\]\/"\t]$|^[^ !#;+\]\/"\t]$
andrey
  • 588
  • 1
  • 8
  • 14
5

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.

Sparers
  • 423
  • 5
  • 15
1

This is an answer, but also a question - does anyone have experience with foreign language or graphics in an SSID? I found the following:

Using Emoji in SSID But if you dig into the governing standards for Wi-Fi, the 802.11 standards maintained by the Institute of Electrical and Electronics Engineers (IEEE), you find something surprising: The only constraint for setting an SSID for a wireless network is that it must be between 1 and 32 octets long, an octet being 8 bits of data.

As of Unicode 14.0, there are 144,697 characters, including characters for hundreds of languages and thousands of emoji symbols. Emoji symbols, interestingly enough, are 2 octets not a single octet like the character A or the number 2. [So too foreign alphabets - each letter take 2 octets.]

So every emoji in your SSID counts as two regular plaintext characters—you can have a 16 emoji SSID, a 30 plaintext character SSID with a single emoji, or any combination between the two that adds up to 32 total octets.

Mechi
  • 11
  • 3
0

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}$/
Emanuele
  • 617
  • 5
  • 16
0

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]+)$
Skunky
  • 1