brython validate form

163 views
Skip to first unread message

eric cuver

unread,
May 15, 2016, 8:58:53 AM5/15/16
to brython
I found nothing for Brython and  validate form I did a test to see how it works with  Brython I'll try to create something better as jquery.validate :


<script type="text/javascript"
</script>
<h1>Ceci est le modèle default/brython.html </h1>

<script type="text/python">

from browser import document, alert

def only_letters(tested_string):
    for letter in tested_string:
        if not letter in "abcdefghijklmnopqrstuvwxyz":
            return False
    return True
    

def validate(ev):
    x  = document["validation"].value
    if len(x) <= 3 :
        alert("le champs doit être remplit minimum de trois lettres")
        ev.preventDefault()
        ev.stopPropagation()
    if only_letters(x) == False :
        alert("le champs doit être remplit de lettre de type a-z")
        ev.preventDefault()
        ev.stopPropagation()
    if len(x) >= 10 :
        alert("le champs dot être remplit de maximum dix lettres")
        ev.preventDefault()
        ev.stopPropagation()
  
  
    


document['valide'].bind('click',validate)
</script>



<body  onload="brython()">
    
    <form name="myForm">
Name: <input id="validation" type="text" name="fname">
<input id = "valide" type="submit" value="Submit">
</form>
    
    
</body>

Kiko

unread,
May 15, 2016, 5:30:46 PM5/15/16
to bry...@googlegroups.com
2016-05-15 14:58 GMT+02:00 eric cuver <amihaco...@gmail.com>:
I found nothing for Brython and  validate form I did a test to see how it works with  Brython I'll try to create something better as jquery.validate :


Is this a question?, an example you want to show to receive feedback?
 
I will provide some feedback.

Some functions could be tested easily in regular Python.


<script type="text/javascript"
</script>
<h1>Ceci est le modèle default/brython.html </h1>

<script type="text/python">

from browser import document, alert

def only_letters(tested_string):
    for letter in tested_string:
        if not letter in "abcdefghijklmnopqrstuvwxyz":
Here I would use letter.lower() in order to allow, for example, possibilities like 'John'
I also will allow spaces in 'abcdef...' in order to allow names like 'Jean François"
            return False
    return True
    

def validate(ev):
    x  = document["validation"].value
    if len(x) <= 3 :
        alert("le champs doit être remplit minimum de trois lettres")
        ev.preventDefault()
        ev.stopPropagation()
    if only_letters(x) == False :
        alert("le champs doit être remplit de lettre de type a-z")
        ev.preventDefault()
        ev.stopPropagation()
    if len(x) >= 10 :
        alert("le champs dot être remplit de maximum dix lettres")
        ev.preventDefault()
        ev.stopPropagation()
  
  
    


document['valide'].bind('click',validate)
</script>



<body  onload="brython()">
    
    <form name="myForm">
Name: <input id="validation" type="text" name="fname">
<input id = "valide" type="submit" value="Submit">
</form>
    
    
</body>

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/7dc48a87-4636-4448-a608-8ea1fbe69467%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eric cuver

unread,
May 15, 2016, 7:39:16 PM5/15/16
to brython
I did not know that the method was put in string.lower Brython if indeed it is better

eric cuver

unread,
May 15, 2016, 7:43:31 PM5/15/16
to brython
I silly I should watch with the dir method


>>> import string
>>> dir(string)
['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__bases__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__file__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__initializing__', '__le__', '__loader__', '__lt__', '__name__', '__ne__', '__new__', '__or__', '__package__', '__repr__', '__setattr__', '__spec__', '__str__', '__subclasshook__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'toString', 'whitespace']
>>> 

Le dimanche 15 mai 2016 23:30:46 UTC+2, kiko (on pybonacci) a écrit :

Kiko

unread,
May 16, 2016, 2:14:59 AM5/16/16
to bry...@googlegroups.com
I discourage you to use the string module for your example as it is
quite expensive to import. The way you are doing it is fine.

2016-05-16 1:43 GMT+02:00, eric cuver <amihaco...@gmail.com>:
> I silly I should watch with the dir method
>
>
>>>> import string
>>>> dir(string)
> ['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__bases__',
> '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__file__',
> '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
> '__init__', '__initializing__', '__le__', '__loader__', '__lt__',
> '__name__', '__ne__', '__new__', '__or__', '__package__', '__repr__',
> '__setattr__', '__spec__', '__str__', '__subclasshook__', '_re', '_string',
>
> 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords',
> 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'toString',
>
> 'whitespace']
>>>>
>
> Le dimanche 15 mai 2016 23:30:46 UTC+2, kiko (on pybonacci) a écrit :
>>
>> 2016-05-15 14:58 GMT+02:00 eric cuver <amihaco...@gmail.com
>> <javascript:>>
>> :
>>
>>> I found nothing for Brython and validate form I did a test to see how it
>>>
>>> works with Brython I'll try to create something better as
>>> jquery.validate :
>>>
>>>
>> Is this a question?, an example you want to show to receive feedback?
>>
>> I will provide some feedback.
>>
>> Some functions could be tested easily in regular Python.
>>
>>
>>> <script type="text/javascript"
>>> src="
>>> https://cdn.rawgit.com/brython-dev/brython/3.2.2/www/src/brython.js">
>>> </script>
>>> <h1>Ceci est le modèle default/brython.html </h1>
>>>
>>> <script type="text/python">
>>>
>>> from browser import document, alert
>>>
>>> def only_letters(tested_string):
>>> for letter in tested_string:
>>> if not letter in "abcdefghijklmnopqrstuvwxyz":
>>>
>> *Here I would use *letter.lower()
>> * in order to allow, for example, possibilities like 'John'*
>> *I also will allow spaces in *'abcdef...'* in order to allow names like
>> 'Jean François"*
>>> email to brython+u...@googlegroups.com <javascript:>.
>>> To post to this group, send email to bry...@googlegroups.com
>>> <javascript:>.
>>> <https://groups.google.com/d/msgid/brython/7dc48a87-4636-4448-a608-8ea1fbe69467%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To post to this group, send email to bry...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/db2daebc-3dfc-41d2-aa02-bb55c28235d3%40googlegroups.com.

eric cuver

unread,
May 16, 2016, 10:37:02 AM5/16/16
to brython
ok I'll do that I work on application mobile and JavaScript with all its library and problem of compatibility killed my time in 1 hour I create to validate in brython and it works 

eric cuver

unread,
May 17, 2016, 5:50:24 AM5/17/16
to brython
i update the code what do you think ?

from browser import document
import re


def only_letters(password):
    if re.search('[A-Za-z0-9\-\_]+',password):  
    return True 
    else:
    return False

def test_email(email):
if re.match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",email,re.IGNORECASE):
       return True
else:
       return False




def validate(ev):
    password  = document["password"].value
    email = document["email"].value
    if len(password) <= 3  :
        document['lettres'].style.display = "block"  
        ev.preventDefault()
        ev.stopPropagation()
    if len(password) >= 3 :
        document['lettres'].style.display = "none"  
        ev.preventDefault()
        ev.stopPropagation()    
    if only_letters(password) == False :
    print(only_letters)
        document['correcte'].style.display = "block"
        ev.preventDefault()
        ev.stopPropagation()
    if only_letters(password) == True :
    print(only_letters)
        document['correcte'].style.display = "none"
        ev.preventDefault()
        ev.stopPropagation()    
    if len(password) >= 10 :
        document['maximum'].style.display = "block"
        ev.preventDefault()
        ev.stopPropagation()
    if len(password) <= 10 :
        document['maximum'].style.display = "none"
        ev.preventDefault()
        ev.stopPropagation()    
    if test_email(email) == False :
        document['format'].style.display = "block"  
        ev.preventDefault()
        ev.stopPropagation()
    if test_email(email) == True :
        document['format'].style.display = "none"  
        ev.preventDefault()
        ev.stopPropagation()


  
  
    


document['connexion'].bind('click',validate)


</script>

<label for="inputemail">Adresse Email</label>
    <input type="email" id="email" name="email" class=" obligatoire form-control input-lg"  placeholder="Adresse email"></input>
    <div id="vide" class="alert alert-danger" hidden="true" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Error:</span>
  Enter a valid email address
</div>
<div id="format" class="alert alert-danger" style=" display: none;" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Error:</span>
  Entrez un format ex: "con...@besafty.com"
</div>

    </div>

<br>

<div class="form-group">

    <label for="inputpassword">Mots de passe</label>
    <input name="password" id="password" type="password" class="obligatoire form-control input-lg"  data-minLength="5" placeholder="Mot de passe"></input>
    <div id="lettres" class="alert alert-danger" style=" display: none;" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Error:</span>
  "le champs doit être remplit minimum de trois lettres"
</div>

<div id="maximum" class="alert alert-danger" style=" display: none;" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Error:</span>
  "le mot de passe doit etre remplit de maximum dix lettres"
</div>
<div id="correcte" class="alert alert-danger" style=" display: none;" role="alert">
  <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
  <span class="sr-only">Error:</span>
  "le champs doit être remplit de lettre avec un format correcte"
</div>

Kiko

unread,
May 18, 2016, 2:53:04 AM5/18/16
to bry...@googlegroups.com
Good job!!!

Importing the re module in brython could be slow so, if performance is very important try to use the javascript engine for regex operations. See an example here:

from browser import window
from javascript import JSConstructor

jsre = JSConstructor(window.RegExp)
mo = jsre(r'a(.*)e').exec('aBCDe')
print(mo)

Obtained from here: https://github.com/brython-dev/brython/blob/master/www/doc/fr/options.md#exemple


eric cuver

unread,
May 20, 2016, 5:28:15 AM5/20/16
to brython
I simplified I begin to understand the brython philosophy it's just a pleasure to program in Brython


from browser import document, window, alert
from javascript import JSConstructor

def check_password(password):
jsre = JSConstructor(window.RegExp)
return jsre('^([a-zA-Z0-9_-]){4,10}$').test(password)

def check_email(email):
jsre = JSConstructor(window.RegExp)
return jsre('^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$').test(email)




def getFocus(ev):
    valuepassword = document.getElementById('password')
    if check_password(valuepassword.value) == False :
    add = valuepassword
add.classList.add('invalid')


def validate(ev):
messagevalidpassword = document.querySelector('#password.validate.valid')
messagevalidemail = document.querySelector('#email.validate.valid')
invamessagevalidpassword = document.querySelector('#password.validate.invalid')
invamessagevalidemail = document.querySelector('#email.validate.invalid')
valuepassword = document.getElementById('password')
valueemail = document.getElementById('email')
if (messagevalidpassword == None) and (messagevalidemail == None) :
alert("vous devez remplir les champs email et mot de passe")
elif invamessagevalidpassword and invamessagevalidemail :
alert("verifier les erreurs dans les chmaps")
elif invamessagevalidpassword or invamessagevalidemail :
alert("Il y a une erreur dans un des champs")
elif (messagevalidpassword != None) and (messagevalidemail == None) :
alert("Verifiez le champs email")
elif (messagevalidpassword == None) and (messagevalidemail != None) :
alert("Verifiez le champs mot de passe")
elif messagevalidpassword and messagevalidemail  :
alert("brvao vous avez tout bien remplis")
Reply all
Reply to author
Forward
0 new messages