Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 10-03-2008, 08:53 AM focus problem
Super Talker

Posts: 134
Trades: 0
this is the code (something basic)

Code:
<html>
<head>
<script type="text/javascript">

function validateField(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
           fld.style.borderColor = 'red';         
        fld.parentNode.getElementsByTagName("span")[0].innerHTML = "You didn't enter a value.\n";        
        fld.focus();
        return false;
    } else if ((fld.value.length < 2) || (fld.value.length > 16)) {
        fld.style.borderColor = 'red';
        fld.parentNode.getElementsByTagName("span")[0].innerHTML = 'The field is the wrong length.';
        fld.focus();
        return false;
    } else if (illegalChars.test(fld.value)) {
        fld.style.borderColor = 'red';
        fld.parentNode.getElementsByTagName("span")[0].innerHTML = 'The field contains illegal characters.\n';         
        fld.focus();
        return false;
    } else {
        fld.style.border = 'none';
        fld.parentNode.getElementsByTagName("span")[0].innerHTML = "";                
        fld.parentNode.getElementsByTagName("span")[1].style.display = "none";  
        return true;      
    } 
}
</SCRIPT>
</head>
<body>
<form method="post">

<fieldset>
    <label for="field1">label1:</label>

    <input
        type="text"
        id="field1"
        name="field1"
        size="20"
        value=""
        onfocus='this.parentNode.getElementsByTagName("span")[1].style.display = "inline";'
        onblur='return validateField(this);' />&nbsp;
    <span style="position:absolute; left:350px; color:red;"></span><br/>
    <span class="hint" style="display:none;">
        hint 1</span>
</fieldset>
<fieldset>
    <label for="field2">label2:</label>
    <input
        type="text"
        id="field2"
        name="field2"
        size="20"
        value=""
        onfocus='this.parentNode.getElementsByTagName("span")[1].style.display = "inline";'
        onblur='return validateField(this);' />&nbsp;
    <span style="position:absolute; left:350px; color:red;"></span><br/>
    <span class="hint" style="display:none;">
        hint 2</span>
</fieldset>
</form>
</body>
</html>
the 'hint' appears when the field get the focus;

the field border becomes red and the error message appears when the field value doesn't pass the test, but the field looses the focus when an error occurs

the focus should stay in the field until the field value passes the field validation.

what is wron with this code here

some help please,

thx
__________________

Please login or register to view this content. Registration is FREE

Check out the Facebook Clone build with Jcow SNS at
Please login or register to view this content. Registration is FREE
, it is free and it always will be
Falcone is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-03-2008, 01:00 PM Re: focus problem
Novice Talker

Posts: 6
Location: Montreal, QC, Canada
Trades: 0
As per the following thread:

http://codingforums.com/archive/index.php?t-56204.html

you need to add a slight delay when calling the focus() method:

setTimeout(function(){fld.focus()}, 10);

instead of

fld.focus().

Hope it works for you. Worked here.
fsav is offline
Reply With Quote
View Public Profile Visit fsav's homepage!
 
Old 10-04-2008, 05:57 AM Re: focus problem
Super Talker

Posts: 134
Trades: 0
it returns the focus to field1 but it puts first the focus on the next field whats is resulting in an error message for the next field saying it is empty.
__________________

Please login or register to view this content. Registration is FREE

Check out the Facebook Clone build with Jcow SNS at
Please login or register to view this content. Registration is FREE
, it is free and it always will be
Falcone is offline
Reply With Quote
View Public Profile
 
Old 10-04-2008, 01:21 PM Re: focus problem
Novice Talker

Posts: 6
Location: Montreal, QC, Canada
Trades: 0
Well I'm not sure you can get the result you want, since you invoke your validateField function with the onblur event, which happens when the focus leaves the field...
fsav is offline
Reply With Quote
View Public Profile Visit fsav's homepage!
 
Reply     « Reply to focus problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13189 seconds with 12 queries