required field

I am creating login page using bootstrap, here is my code

  <form class="form-signin" action="firstLogin.action" method="post">
    <h2 class="form-signin-heading">Please sign in</h2>
    <input type="text" class="form-control" name="username" placeholder="User Name"     
       required="required" autofocus>
    <br>
    <input type="password" class="form-control" name="password" required="required" 
       placeholder="Password">
    <br>
    <div class="btn-group" data-toggle="buttons-radio">
   <button type="button" class="btn btn-primary" style="width: 150px">Admin</button>
   <button type="button" class="btn btn-primary" style="width: 150px">User</button> 
    </div>
   <div><br></div>

   <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
 </form>

I want to make radio button field as required field . any idea how to do it.?

share improve this question
 
 
You could just check if it is empty or not, in case of empty stop to going further, if not check format, type of data... and go on –  Llopis  Feb 19 '14 at 13:18
 
Thanks .. but can we do it as <input type="text" class="form-control" name="username" placeholder="User Name" required="required"> stackoverflow.com/users/2886003/llopis –  Ajinkya  Feb 19 '14 at 13:20 

2 Answers

up vote 5 down vote accepted

I got it!

All you need to do is

-create two divs, one for data-toggle and child her with btn-group

Therefore, you get a group of buttons with a radio inside. set those radio btns to

class="sr-only"

<form class="form-signin" action="firstLogin.action" method="post">
 <h2 class="form-signin-heading">Please sign in</h2>

<input type="text" class="form-control" name="username" placeholder="User Name" required autofocus>
<br>
<input type="password" class="form-control" name="password" required placeholder="Password">
<br>


                <div data-toggle="buttons">
                <div class="btn-group">

                    <label class="btn btn-primary">
                        <input type="radio" name="type" id="type" value="admin" class="sr-only" required>Admin
                    </label>
                    <label class="btn btn-primary">
                        <input type="radio" name="type" id="type" value="user" class="sr-only" required>User
                    </label>
                </div>
                </div>
            </div>

<br>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>

http://jsfiddle.net/nteb4/23/

It's working on bootstrap3

share improve this answer
 
1 
it doesnt work unless you select "user" if you click on "admin" doesnt let you submit the form –  jpganz18  Jan 7 '15 at 10:35
 
@jpganz18 fixed now –  Abzoozy  Mar 5 '15 at 16:50

In order to make input required, the element must be an input element. Change your buttons to inputs, give them a name, add required, and change the type to 'radio', and required should work (see this fiddle):

<div>
    <input type="radio" name="radio-choice" required>Admin</input>
    <input type="radio" name="radio-choice" required>User</input> 
</div>

Unfortunately this means they don't look like cool buttons anymore. You can style radio buttons to look like what you want, and required will still stop the form from submitting, but you won't see the popup warning to select an option (see this fiddle).

So, for what you want, it looks like you'll have to do some basic programming. Here's a fiddle with a basic check in place using your original buttons.

share improve this answer
 
 
I know this .. but im asking about can we do it as im saying, by using bootstrap radio buttons. –  Ajinkya  Feb 20 '14 at 7:42 
 
YES it works great using bootstrap too i recently used it and is working with just a simple required attribute –  TaraGurung  Nov 25 '15 at 8:23
 
I wish I could vote twice here. This answer is so much simpler than the other popular answer. –  Brian Ruff  Feb 23 at 22:42
`Required field 'client_protocol' is unset!` 这个错误通常在使用 Thrift 相关服务时出现,它表明客户端在与服务器通信时,没有设置必要的 `client_protocol` 字段。以下是一些可能的解决方法: ### 检查客户端代码 在使用 Thrift 客户端时,需要确保正确设置了协议。以下是一个 Python 示例,展示了如何正确设置协议: ```python from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from your_thrift_gen_module import YourService # 替换为实际的 Thrift 生成模块和服务名 # 创建传输层 transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) # 创建协议层 protocol = TBinaryProtocol.TBinaryProtocol(transport) # 创建客户端 client = YourService.Client(protocol) # 打开传输 transport.open() # 调用服务方法 result = client.your_method() # 关闭传输 transport.close() ``` 在上述代码中,`TBinaryProtocol.TBinaryProtocol` 用于设置二进制协议。根据实际情况,你可能需要使用其他协议,如 `TJSONProtocol` 等。 ### 检查服务器端代码 服务器端也需要正确配置协议,以匹配客户端的设置。以下是一个 Python 服务器端示例: ```python from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from your_thrift_gen_module import YourService # 替换为实际的 Thrift 生成模块和服务名 from your_service_handler import YourServiceHandler # 替换为实际的服务处理类 # 创建处理器 handler = YourServiceHandler() processor = YourService.Processor(handler) # 创建传输层 transport = TSocket.TServerSocket(host='localhost', port=9090) tfactory = TTransport.TBufferedTransportFactory() # 创建协议层 pfactory = TBinaryProtocol.TBinaryProtocolFactory() # 创建服务器 server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print('Starting the server...') server.serve() print('Server has stopped.') ``` 确保服务器端和客户端使用相同的协议。 ### 检查 Thrift 版本兼容性 确保客户端和服务器端使用的 Thrift 版本一致,不同版本的 Thrift 可能会导致协议不兼容问题。 ### 检查配置文件 如果使用配置文件来配置 Thrift 客户端或服务器,确保配置文件中正确设置了协议相关参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值