Providing validations to HTML Form

201 views
Skip to first unread message

Praveen Kumar

unread,
Sep 8, 2012, 6:21:52 AM9/8/12
to mi...@dartlang.org
Hi, 
I am new to Dart language , I would like to know how to provide validations to the HTML Form just like we provide using JavaScripts.

For instance:

<form name='registration' onSubmit="return formValidation();">
<ul>
<li><label for="userid">User id:</label></li>
<li><input type="text" name="userid" size="12" /></li>
<li><label for="passid">Password:</label></li>
<li><input type="password" name="passid" size="12" /></li>
<li><input type="submit" name="submit" value="Submit" /></li>
</ul>
</form>

In JavaScript we do as following

function formValidation()
{
var uid = document.registration.userid;
var passid = document.registration.passid;
if(userid_validation(uid,5,12))
{
if(passid_validation(passid,7,12))
{
}
}
return false;
function userid_validation(uid,mx,my)
{
var uid_len = uid.value.length;
if (uid_len == 0 || uid_len >= my || uid_len < mx)
{
alert("User Id should not be empty / length be between "+mx+" to "+my);
uid.focus();
return false;
}
return true;
}
function passid_validation(passid,mx,my)
{
var passid_len = passid.value.length;
if (passid_len == 0 ||passid_len >= my || passid_len < mx)
{
alert("Password should not be empty / length be between "+mx+" to "+my);
passid.focus();
return false;
}
return true;
alert('Form Succesfully Submitted');
}

how to this in dart ?



Bob Nystrom

unread,
Sep 20, 2012, 12:48:26 PM9/20/12
to mi...@dartlang.org
In most cases, the way you accomplish something in JS is the same or nearly the same to what you would write in Dart.

Skimming your code, the main differences I can think of are:
  • In Dart, you'll do window.alert() instead of just alert().
  • To get the form element, you'll do (I think) query('registration') instead of window.registration.
Try that and if it doesn't work, let us know what problems you run into.

Cheers!

- bob




--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 

John Evans

unread,
Sep 20, 2012, 1:47:13 PM9/20/12
to mi...@dartlang.org
I think it might be this Bob:

document.query('[name="registration"]')
From: http://synonym.dartlang.org/  :)
Reply all
Reply to author
Forward
0 new messages