Skip to content

Instantly share code, notes, and snippets.

@adityasuseno
Last active June 27, 2019 18:40
Show Gist options
  • Save adityasuseno/199b1e39c2a2b8aea146ec54bc3f85e3 to your computer and use it in GitHub Desktop.
Save adityasuseno/199b1e39c2a2b8aea146ec54bc3f85e3 to your computer and use it in GitHub Desktop.
Parser Python dan Java Untuk User ID Supaya Hanya Mengandung Alphanumeric dan Tidak Kurang dari 4 Karakter Maupun Melebihi 20 Karakter
public class UserID_Parser
{
public static void main(String[] args)
{
System.out.println(isValidUserID(args[0]));
}
public static boolean isValidUserID(String str)
{
boolean q = false;
int len = str.length();
if (len < 4 || len > 20)
{
q = false;
}
else
{
for(int n = 0; n<len; n++)
{
if (Character.isLowerCase(str.charAt(n)) && Character.isLetterOrDigit(str.charAt(n)))
{
q = true;
continue;
}
else
{
q = false;
break;
}
}
}
return q;
}
}
#!/usr/bin/env python3
# UserID hanya boleh alphanumeric huruf kecil dan angka
allowedletters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
# Input UserID ke variabel userid
userid = str(input("Masukan User ID : "))
if len(userid)>3 and len(userid)<21:
# Parsing...
for n in range(0, len(userid)):
if userid[n] in allowedletters[:]:
acceptedflag = True
continue
else:
acceptedflag = False
break
if acceptedflag == True:
print("UserID Accepted") # Diterima
else:
print("UserID hanya boleh mengandung huruf kecil dan angka") # Ditolak
else:
print("UserID minimal 4 karakter dan maksimal 20 karakter") # Ditolak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment