Важно не атрибут, а свойство , и его значение является логическим.
Вы можете установить его с помощью
document.getElementById("edName").required = true;
Вы можете использовать str.isdigit
и str.isalpha
:
if a.isalpha():
#do something
elif a.isdigit():
#do something
help на str.isdigit
:
>>> print str.isdigit.__doc__
S.isdigit() -> bool
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
help на str.isalpha
:
>>> print str.isalpha.__doc__
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
Вы можете использовать a.isalpha (), a.isdigit (), a.isalnum (), чтобы проверить, состоит ли из букв, цифр или комбинации чисел и букв соответственно.
if a.isalpha(): # a is made up of only letters
do this
if a.isdigit(): # a is made up of only numbers
do this
if a.isalnum(): # a is made up numbers and letters
do this
Python docs расскажет вам более подробно о методах, которые вы можете вызвать для строк.
Видите, что вы используете input () в примере тура, вы должны знать, что вход всегда дает вам строку. И вам нужно направить его на правильный тип: EG: Int или Float.
def isint(input):
return input.isdigit()
def isfloat(input):
try:
return float(input) != None;
except ValueError:
return False;
def isstr(input):
if not isint(input) and not isfloat(input):
return True
return False
print isint("3.14")
print isfloat("3.14")
print isstr("3.14")