0

I have created a django application. Using this app users can be created or deleted. But while creating a user, when we leave 1 of the field (Employee ID Field) null its not getting submitted and its throwing this error - invalid literal for int() with base 10: '' . So i guess validating that field alone and submitting that form, only if employee ID Field is non null can solve my problem. So for doing that i created a javascript and wants that js function to be called before submitting. Need to submit that form only if that field is non-empty. The alert box is coming saying to Enter the employee ID, but once i click ok to alert box, the same error comes again.Can somebody help me to solve this. I am quite new to Django and programming. I will paste my code here.

Add New User to Employee List
</h4><br><br><br>
<form name="myForm" action="http://10.1.0.90:8080/createEmployee/" method="POST">
<br>Name: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="userName"  style="color: black; background-color: #BDBDBD" /><br /><br>
Designation:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<select name="designation"  style="color: black; background-color: #BDBDBD" >
 <option value="Trainee" >Trainee</option>
 <option value="Software Engineer Trainee">Software Engineer Trainee</option>
 <option value="Software Engineer">Software Engineer</option>
 <option value="Senior Software Engineer">Senior Software Engineer</option>
 <option value="Project Manager">Project Manager</option>
 <option value="System Administrator">System Administrator</option> 
 <option value="Tester">Tester</option>
</select><br>
<br>
EmployeeID:<input id ="empid" type="text" name="employeeID"  style="color: black; background-color: #BDBDBD" /><br><br>
Contact Number: <input type="text" name="contactNumber"  style="color: black; background-color: #BDBDBD" /><br><br>
Project:&nbsp;&nbsp;&nbsp;<input type="text" name="project"  style="color: black; background-color: #BDBDBD" /><br><br>
Location:&nbsp;<input type="text" name="location"  style="color: black; background-color: #BDBDBD" /><br><br>
E-mail:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="email"  style="color: black; background-color: #BDBDBD" /><br><br>
Skills:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="skills"  style="color: black; background-color: #BDBDBD" /><br><br>
Date Of Join:&nbsp;<input type="text" id="datepicker" name="dateOfJoin"  style="color: black; background-color: #BDBDBD"  /><br><br>
<br><br>
<input type="submit" value="   Submit  " onclick="isEmpty()" /><br />
</form> 
<script type="text/javascript">
function isEmpty(){
    if ((document.myForm.EmployeeID.value.length==0)) 
        {
            alert("Please Enter Employee ID");
                    return true;
        }
    else 
        { 
        return false; 
        }

}

1
  • 1
    Seriously, use proper CSS for spacing, not all those nbsps. Commented Mar 23, 2011 at 8:34

1 Answer 1

2

mistake: "if ((" two brackets opened

--- UPDATE ---

First of all, if you get an error about converting empty or null to a number, that means you have your code wrong. Fixing it by having additional verification at the javascript means you're making very nasty workaround without fixing error itself - if javascript fails (e.g. no javascript enabled in the browser) user still gets 50x error.

So, fix your form to have this field required. IMO you have EmployeeID defined as non-required field in the form and then you assume, that this field is required and tries to use it.

If you will fix your application (backend) code, then you can add verification at the frontend (javascript).

Proper way of attaching a form verification is to attach it to the "on-submit" form handler. If this handler will return "false" form will not be submitted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.