3

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.
  • 61
  • 1
  • 5

4 Answers4

4

I use this regex to match SSID:

^[^!#;+\]\/"\t][^+\]\/"\t]{0,30}[^ !#;+\]\/"\t]$|^[^ !#;+\]\/"\t]$
andrey
  • 568
  • 1
  • 7
  • 14
3

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
  • 393
  • 3
  • 15
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
  • 559
  • 4
  • 14
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